Newbie: Replacing double newlines

Discussion in 'Perl Misc' started by Jensen, Aug 8, 2008.

  1. Jensen

    Jensen Guest

    Hello:

    This works for me to replace single newline characters with spaces:

    perl -pe 's/\n/ /g' test2

    in a file.

    However, when I want to replace just the double newlines (two of them in
    sequence), with:

    perl -pe 's/\n\n/ /g' test2

    it doesn't work. Same if I'm using \012 for the newline (shown by the od
    command in UNIX).

    And they're there, I see them with say:

    od -cb test2

    or with OpenOffice or other editors.

    What gives?

    Thanks.
    Jensen, Aug 8, 2008
    #1
    1. Advertising

  2. Jensen

    Ben Morrow Guest

    Quoth Jensen <>:
    > Hello:
    >
    > This works for me to replace single newline characters with spaces:
    >
    > perl -pe 's/\n/ /g' test2
    >
    > in a file.
    >
    > However, when I want to replace just the double newlines (two of them in
    > sequence), with:
    >
    > perl -pe 's/\n\n/ /g' test2
    >
    > it doesn't work. Same if I'm using \012 for the newline (shown by the od
    > command in UNIX).


    -p reads files line-by-line by default, so no one line will ever contain
    two newline characters. Try

    perl -0777 -pe 's/\n\n/ /g' test2

    -0777 is equivalent to BEGIN { $/ = undef }: see perlrun.

    Ben

    --
    You poor take courage, you rich take care:
    The Earth was made a common treasury for everyone to share
    All things in common, all people one.
    'We come in peace'---the order came to cut them down. []
    Ben Morrow, Aug 9, 2008
    #2
    1. Advertising

  3. Jensen

    Ben Bullock Guest

    On Fri, 08 Aug 2008 18:37:27 -0400, Jensen wrote:

    > Hello:
    >
    > This works for me to replace single newline characters with spaces:
    >
    > perl -pe 's/\n/ /g' test2
    >
    > in a file.
    >
    > However, when I want to replace just the double newlines (two of them in
    > sequence), with:
    >
    > perl -pe 's/\n\n/ /g' test2
    >
    > it doesn't work.


    This reads it line by line, so since there isn't a single line with two \n
    on it, it doesn't work.

    The best solution I could think of for your problem was something like this:

    perl -pe's/\n/\\n/g' test.txt|perl -pe 's/(\\n){2,}/\\n/g' | perl -pe 's/\\n/\n/g'

    Here I'm assuming that you didn't really want to turn \n\n into a space.

    But someone else can probably do it better than that.
    Ben Bullock, Aug 9, 2008
    #3
  4. Jensen <> wrote:
    > Hello:
    >
    > This works for me to replace single newline characters with spaces:
    >
    > perl -pe 's/\n/ /g' test2
    >
    > in a file.
    >
    > However, when I want to replace just the double newlines (two of them in
    > sequence), with:
    >
    > perl -pe 's/\n\n/ /g' test2
    >
    > it doesn't work.



    Of course not.

    Have you read the documentation for the switches you are using?

    perldoc perlrun

    causes Perl to assume the following loop around your program, which
    makes it iterate over filename arguments somewhat like B<sed>:


    LINE:
    while (<>) {
    ... # your program goes here
    } continue {
    print or die "-p destination: $!\n";
    }


    ( While you are in perlrun, have a look at the -0 (zero) switch also. )


    > What gives?



    Your program never has more than one line in $_, so it cannot possibly
    match a multiline string.


    perldoc -q

    I'm having trouble matching over more than one line. What's wrong?


    If the file is small enough to fit in memory, then:

    perl -0777pe 's/\n\n/ /g' test2


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Aug 9, 2008
    #4
  5. Jensen

    Jensen Guest

    Ben Morrow wrote:

    > -p reads files line-by-line by default, so no one line will ever contain
    > two newline characters. Try
    >
    > perl -0777 -pe 's/\n\n/ /g' test2
    >
    > -0777 is equivalent to BEGIN { $/ = undef }: see perlrun.


    Thank you very much, that did it.
    Jensen, Aug 9, 2008
    #5
  6. Jensen

    magloca Guest

    Ben Morrow wrote:

    > -0777 is equivalent to BEGIN { $/ = undef }: see perlrun.


    Wow, that's obscure even for Perl. :D

    m.
    magloca, Aug 9, 2008
    #6
  7. Jensen

    Ted Zlatanov Guest

    On Sat, 09 Aug 2008 13:01:36 +0200 magloca <> wrote:

    m> Ben Morrow wrote:
    >> -0777 is equivalent to BEGIN { $/ = undef }: see perlrun.


    m> Wow, that's obscure even for Perl. :D

    You don't want to know what -0666 does (one side effect is, the hex()
    function is redefined to do something entirely different).

    Ted
    Ted Zlatanov, Aug 11, 2008
    #7
  8. Jensen

    Ben Morrow Guest

    Quoth Ted Zlatanov <>:
    > On Sat, 09 Aug 2008 13:01:36 +0200 magloca <> wrote:
    >
    > m> Ben Morrow wrote:
    > >> -0777 is equivalent to BEGIN { $/ = undef }: see perlrun.

    >
    > m> Wow, that's obscure even for Perl. :D
    >
    > You don't want to know what -0666 does (one side effect is, the hex()
    > function is redefined to do something entirely different).


    I think this post was missing a smiley :).

    Ben

    --
    I touch the fire and it freezes me, []
    I look into it and it's black.
    Why can't I feel? My skin should crack and peel---
    I want the fire back... BtVS, 'Once More With Feeling'
    Ben Morrow, Aug 11, 2008
    #8
  9. Jensen

    Ted Zlatanov Guest

    On Mon, 11 Aug 2008 17:19:22 +0100 Ben Morrow <> wrote:

    BM> Quoth Ted Zlatanov <>:
    >> On Sat, 09 Aug 2008 13:01:36 +0200 magloca <> wrote:
    >>

    m> Ben Morrow wrote:
    >> >> -0777 is equivalent to BEGIN { $/ = undef }: see perlrun.

    >>

    m> Wow, that's obscure even for Perl. :D
    >>
    >> You don't want to know what -0666 does (one side effect is, the hex()
    >> function is redefined to do something entirely different).


    BM> I think this post was missing a smiley :).

    }:)

    Ted
    Ted Zlatanov, Aug 11, 2008
    #9
  10. Ted Zlatanov <> wrote:
    > On Sat, 09 Aug 2008 13:01:36 +0200 magloca <> wrote:
    >
    > m> Ben Morrow wrote:
    >>> -0777 is equivalent to BEGIN { $/ = undef }: see perlrun.

    >
    > m> Wow, that's obscure even for Perl. :D
    >
    > You don't want to know what -0666 does (one side effect is, the hex()
    > function is redefined to do something entirely different).



    Should that be added to this list?

    http://nostalgia.wikipedia.org/wiki/Internet_humor/Number_of_the_beast


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Aug 12, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darpan
    Replies:
    3
    Views:
    12,910
    Darpan
    Mar 2, 2005
  2. Sydex
    Replies:
    12
    Views:
    6,427
    Victor Bazarov
    Feb 17, 2005
  3. Tom
    Replies:
    5
    Views:
    296
  4. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    251
    Chris Hohmann
    Apr 11, 2005
  5. Ga
    Replies:
    1
    Views:
    111
    Anno Siegel
    Dec 15, 2003
Loading...

Share This Page