Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^2: Removing double carriage return

by dragooneye (Novice)
on Aug 22, 2011 at 18:35 UTC ( #921714=note: print w/replies, xml ) Need Help??

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

My updated code that seems to work. Thanks again pvaldes for your help!

pvaldes' code:
while (<>) { next if $_ =~ m/\n{1}/; if ($_ =~ m/\n{2}){s/\n{2}/\n/gs} elsif ($_ =~ m/\n{3,}){s/\n{3,}/\n\s/gs} }
while (<>) { if ($_ =~ m/\n{1}/) { } if ($_ =~ m/\n{2}/){ s/\n{2}/\n/gs; print; } elsif ($_ =~ m/\n{3,}/){ s/\n{3,}/\n/gs; print; } }
Above does not work. Below now works (prob a crude way of doing it. Please reply if you have a more elegant way):
while (<>) { if ($_ =~ m/\S\n{2}\S/){ s/(\S)\n{2}(\S)/$1\n$2/gs; print; } elsif ($_ =~ m/\n{3,}/){ s/\n{3,}/\n\n/gs; print; } }

Replies are listed 'Best First'.
Re^3: Removing double carriage return
by Cristoforo (Curate) on Aug 23, 2011 at 04:58 UTC
    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

      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.
Re^3: Removing double carriage return
by pvaldes (Chaplain) on Aug 22, 2011 at 20:31 UTC
    That's better than my code yep, like this you catch a last case that I was missing: a file without any \n. Your code is basically the same as this, but probably don't hurt if you add a last small else only to caught this cases and help in future reviews
    while (<>) { if ($_ =~ m/\S\n{2}\S/){ s/(\S)\n{2}(\S)/$1\n$2/gs; print; } elsif ($_ =~ m/\n{3,}/){ s/\n{3,}/\n\n/gs; print; } else {print;} # do nothing (when you have 0 or 1 \n) }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://921714]
[Corion]: Yay. I've now published (at $work) that random Excel workbook where on one sheet you enter your SQL and on the other sheet, you have a button and receive the results. I'm so evil in enabling cow-orkers to self-supply ;)
[Corion]: It's close to my Perl script where you paste in the SQL and get the result as XLSX file, but I guess the Excel sheet is more user-friendly :)
[choroba]: What drivers does it support? ;-)
[erix]: sounds cool -- but now you have to give SQL lessons?

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2018-07-18 13:02 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (392 votes). Check out past polls.