Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by scottysb (Initiate)
on May 14, 2008 at 10:15 UTC ( #686490=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://686490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2021-05-06 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (74 votes). Check out past polls.

    Notices?