开发者

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 expressi开发者_运维知识库on in perl? Anything like the following just shows no result at all.

echo $'a\n\nb'|perl -p -e 's/\n\n/\n/s'


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜