http://www.perlmonks.org?node_id=686490


in reply to What is the best way to remove(delete) specific lines from a very large file?

Here's an example of how you can just keep one file with lets say 10 lines. Once the file reaches the max size, new entries are put at the end and the first line is removed.
#!/usr/bin/perl my $FILE = $ARGV[0]; my $MAXSIZE=10; exit -1 if ! -f $FILE; $num_lines = 0; open FILE, ">$FILE"; for (;;) { chomp($mytime=`date +%T`); my $line = "$mytime"; $line .= ";" . sprintf "%.2f", rand(10); $line .= ";" . sprintf "%.2f", rand(20); $line .= ";" . sprintf "%.2f", rand(30); $line .= ";" . sprintf "%.2f", rand(40); if($num_lines < $MAXSIZE){ push @array, "$line\n"; $num_lines++; }else{ shift @array; push @array, "$line\n"; } #Get the end position of the file seek(FILE, 0, 2); $endpos = tell(FILE); seek(FILE, 0, 0);#Seek to start print FILE @array; #For the rest of the bytes print " " $curpos = tell(FILE); for(my $cnt=$curpos;$cnt<$endpos;$cnt++){ print FILE " "; } sleep 1; }
  • Comment on Re: What is the best way to remove(delete) specific lines from a very large file?
  • Download Code