Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Keep It Simple, Stupid
 
PerlMonks  

Re: remove blank lines with regex

by Biker (Priest)
on May 21, 2002 at 13:18 UTC ( #168113=note: print w/ replies, xml ) Need Help??


in reply to remove blank lines with regex

Consider this:

# Open both IN_FILE and OUT_FILE here! while(<IN_FILE>) { chomp; next unless length; print OUT_FILE "$_\n"; }

I always come back to the following:

Do not slurp in a file into an array. Someday that input file will be huge. And that will happen when used for
production data. And you're on vacation. And you'll have to come in to the office during that sunny day on the beach.

Read the input file line by line and act upon each line (here by potentially writing it to the output file.)

Everything went worng, just as foreseen.


Comment on Re: remove blank lines with regex
Download Code
Re: Re: remove blank lines with regex
by Joost (Canon) on May 21, 2002 at 13:35 UTC
    Do not slurp in a file into an array. Someday that input file will be huge.

    I second that, and here is my 2-cent solution:

    perl -wnl -e 'print $_ unless /^$/' infile >outfile

    see also:

    perldoc perlrun perldoc perlre
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: Re: remove blank lines with regex
by amarceluk (Beadle) on May 21, 2002 at 13:41 UTC
    Thanks, good advice. I got the "slurping files into an array" snippet from something in the Q&A on regex, but I will avoid it henceforth!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-04-23 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (548 votes), past polls