Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Removing double carriage return

by Cristoforo (Curate)
on Aug 23, 2011 at 04:58 UTC ( #921824=note: print w/replies, xml ) Need Help??


in reply to Re^2: Removing double carriage return
in thread Removing double carriage return

From the command line, this replaces 3 or more newlines with 2 newlines or replaces exactly 2 newlines with 1 newline.

perl -i.bak -0777 -pe 's/(\n{3,}|\n\n)/2 == length $1 ? "\n" : "\n\n"/eg' inputfile

Notice that this looks for the longest match first (so that 2 newlines won't match more than 2, i.e. 3 or more).

Update: That could be simplified to:

perl -i.bak -0777 -pe 's/\n+/2 < length $& ? "\n\n" : "\n"/ge' inputfile

Replies are listed 'Best First'.
Re^4: Removing double carriage return
by dragooneye (Novice) on Aug 26, 2011 at 17:29 UTC
    Sorry for the late reply. Thanks Cristoforo for the one liner solution. I'll need to read up on the slurp switch. I hope that this will help others.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://921824]
help
Chatterbox?
[marto]: good morning all
[Corion]: Hi marto!
[Corion]: I hope you're well!
[Discipulus]: good morning monks!
[marto]: Corion, I've been better, but things are improving in many cases :) Simply not enough time/rest :)
[Corion]: Hi Discipulus!
[marto]: hopefully you guys are also well :)

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2017-07-27 07:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (404 votes). Check out past polls.