tags:

views:

97

answers:

3

I've got a document containing empty lines (\n\n). They can be removed with sed:

echo $'a\n\nb'|sed -e '/^$/d'

But how do I do that with an ordinary regular expression in perl? Anything like the following just shows no result at all.

echo $'a\n\nb'|perl -p -e 's/\n\n/\n/s'
+8  A: 

You need to use s/^\n$//. Input is read by line so you will never get more than one newline. Instead, eliminate lines that do not contain any other characters. You should invoke perl using

perl -ne 's/^\n$//; print'

No need for the /s switch.

The previous version of this answer was horribly wrong.

Sinan Ünür
This is nicer than slurping in the entire file at once, which is not a good idea if the file is really large.
Ether
+1  A: 

The input is three separate lines, and perl with the -p option only processes one line at time.

The workaround is to tell perl to slurp in multiple lines of input at once. One way to do it is:

echo $'a\n\nb' | perl -pe 'BEGIN{$/=undef}; s/\n\n/\n/'

Here $/ is the record separator variable, which tells perl how to parse an input stream into lines.

mobrule
+5  A: 

The narrower problem of not printing blank lines is more straightforward:

$(input) | perl -ne 'print if /\S/' 

will output all lines except the ones that only contain whitespace.

mobrule