Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^2: how to use matching operator on newlines

by extremely (Priest)
on Jan 03, 2007 at 21:19 UTC ( #592834=note: print w/replies, xml ) Need Help??

in reply to Re: how to use matching operator on newlines
in thread how to use matching operator on newlines

One option to avoid slurping and would be to set $/ to the double return:

perl -i -pe 'BEGIN{$/="\n\n"} s/\n\n/\n/;' foo.txt

Another option would be to set $/ and $\ (Yay, output format!) and chomp it:

perl -i -pe 'BEGIN{$/="\n\n";$\="\n"} chomp;' foo.txt

-i added back in per kyle's post. Also, ++ to ikegami, too!

$you = new YOU;
honk() if $you->love(perl)

Replies are listed 'Best First'.
Re^3: how to use matching operator on newlines
by ikegami (Pope) on Jan 03, 2007 at 21:37 UTC

    perl -pe 'BEGIN{$/="\n\n";$\="\n"} chomp;' foo.txt
    can be shortened to
    perl -ple 'BEGIN{$/="\n\n"}' foo.txt

Re^3: how to use matching operator on newlines
by kyle (Abbot) on Jan 03, 2007 at 21:44 UTC

    You really need the -i option to edit the file in place as the OP did (I love the irrational number options "-pi -e"). Avoiding slurping is good (especially if it's a large file). The chomp usage is clever, but I think I prefer your first line. Not only does it have one less weird variable, but the s also makes it a lot more obvious what it's doing. On the other hand, maybe I shouldn't be worried about the readability of a one-liner.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://592834]
[Corion]: Oooh - I had another devious idea - "Host C" - a C language where every struct is 4K in size. This makes memory and disk access incredibly fast ;)
[Corion]: (because everything is aligned to a memory page and all pages can be read+written directly from disk without buffering)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-04-25 09:00 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (449 votes). Check out past polls.