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

gitarwmn has asked for the wisdom of the Perl Monks concerning the following question:


I have a file in which I am trying to parse out any blank lines with a subsitition. The problem is I'm not sure how to get rid of the blank lines.
the file looks something like this
#fred #barney #wilma #betty

So how can I get rid of the blank line(s)in the file when they are just a newline within the text file. Thanks

Replies are listed 'Best First'.
Re: newlines in regular expressions
by tachyon (Chancellor) on Dec 07, 2004 at 06:29 UTC

    The keys as it were are to note that \n\n is a blank line or if you read a file line by line then a blank line will match m/^\s*$/

    perl -pi.bak -e 's/\n{2,}/\n/' file.txt
    perl -pi -e 's/^\s*\z//' file.txt perl -ne 'print unless m/^\s*$/' file.txt > noblanks.txt

    Update

    Updated as per sgifford's comments.

    cheers

    tachyon

      A few nits: The first answer requires that Perl read in slurp mode, and needs a g modifier to replace all occurences:
      perl -p -e 'BEGIN { undef $/ } s/\n{2,}/\n/g'

      Also, the two examples behave differently in the face of lines that contain only blank characters, like space and tab. The second will delete them, while the first will leave them intact. You can make the second leave them intact by using m/^$/; to make the first delete whitespace-only lines I think s/\n\s*\n/\n/g will work, but I haven't thought it through yet.

        perl -p -e 'BEGIN { undef $/ } s/\n{2,}/\n/g'

        Which can be shortened to:

        perl -p0777e 's/\n{2,}/\n/g'
        See perldoc perlrun for details of perl's -0 option.

        perl -pi -e 's/^\s*\z//' file.txt deals with blank lines with other whitespace.

        It is as you say.

Re: newlines in regular expressions
by conrad (Beadle) on Dec 07, 2004 at 10:29 UTC

    None of the non-<>-print solutions I see here will cope with a single opening blank line since they all search for multiple \n characters. This does the trick using the /m modifier, which allows ^ to match the beginning of a line anywhere in your string:

    perl -p0777e 's/^\n+//mg'

Re: newlines in regular expressions
by Crian (Curate) on Dec 07, 2004 at 10:18 UTC

    Perhaps it could be useful (in case that these lines are not really empty because they contain whitespace) to use a script like this:

    while (<>) { print unless m~^\s*\n$~; }

    This is surely not the most elegant solution, but it should do the task.