Simon A. Eugster
Simon A. Eugster

Reputation: 4264

How do I replace multiple newlines with a single one with Perl's Regular Expressions?

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'

Upvotes: 6

Views: 3167

Answers (3)

Sinan Ünür
Sinan Ünür

Reputation: 118118

You need to use s/^\n\z//. 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\z//; print'

No need for the /s switch.

Upvotes: 13

mob
mob

Reputation: 118595

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.

Upvotes: 6

mob
mob

Reputation: 118595

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.

Upvotes: 3

Related Questions