Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Removing double carriage return

by pvaldes (Chaplain)
on Aug 20, 2011 at 01:40 UTC ( [id://921351]=note: print w/replies, xml ) Need Help??


in reply to Removing double carriage return

match 3 or more carriage returns... maybe like this? =~ m/\n{3,}/ ... so expanding the script provided from GrandFather
while (<>) { s/\n\n/\n/gs; print; }
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} }
or something like this...

Replies are listed 'Best First'.
Re^2: Removing double carriage return
by Anonymous Monk on Aug 20, 2011 at 01:48 UTC
    A program that reads a file, line by line, will never encounter more than one carriage return

      This is the why GrandFather is using "local $/;" in his script which is referenced by pvaldes reply.

      Update: I didn't read the comment of pvaldes before he updated it, thanks Anonymous for clarifying this issue

        This is the why GrandFather is using "local $/;" in his script which is referenced by pvaldes reply.

        The proper way to reference GrandFathers reply is to respond to GrandFathers reply and copy/paste the most critical portion (local $/)

        What pvaldes has done is update his reply without notice

        This is his original node

        match 3 or more carriage returns... maybe like this? <code>if =~ m/[\n]{3,}/</code>
Re^2: Removing double carriage return
by dragooneye (Novice) on Aug 22, 2011 at 18:21 UTC

    Thanks pvaldes!

    This is a great addition to GrandFather's code. Unfortunately I can't get it to work exactly as you wrote.

    Minor issues: missing closing slashes for m/\n{2} and m/\n{3,} per my Perl system.

    My Perl system also could not interpret the \s escape char.

    After fixing the minor issues, the resultant file turns out blank. However, this will jumpstart my attempts to get a working script. I want the following logic. If one carriage return, do nothing. If two carriage returns, substitute with one. If three or more carriage returns, substitute all with 2.

    I'll post it when I can get it working.

Re^2: Removing double carriage return
by dragooneye (Novice) on Aug 22, 2011 at 18:35 UTC

    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} }
    Mine:
    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; } }
      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.
      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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://921351]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found