Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Remove line from file based on numeric column value

by aaron_baugher (Curate)
on May 25, 2015 at 21:59 UTC ( [id://1127725]=note: print w/replies, xml ) Need Help??


in reply to Remove line from file based on numeric column value

Instead of thinking in terms of "removing lines," think of it as, "print the rest of the lines, skipping certain ones." Also, the array you read the file into is not the file. You have to print the results back out. The best method is usually to read line-by-line from the input file, printing to a new file as you go, then move that new file into place if necessary. So in pseudocode:

open inputfile for reading open outputfile for writing while get a line from inputfile split it into fields if the 8th field is >= -10 and <= 10 print the line to outputfile close the files move outputfile to inputfile

If, for some reason, you can't print the results to a new file (perhaps you don't have enough disc space to accommodate two copies of the data at once), look into the tie function and related modules. They allow you to tie an array variable to a file, so that each line is one element of the array. Then you could splice elements you don't want out of the array, and those lines will be removed from the file. But that method is more advanced, usually unnecessary, and more dangerous -- the "write to a new file then move it" method gives you a chance to make sure the new copy is what you wanted before wiping out the old copy.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-03-28 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found