Newbie: Replacing double newlines

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

  1. Jensen

    Jensen Guest


    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?

    Jensen, Aug 8, 2008
    1. Advertisements

  2. Jensen

    Ben Morrow Guest

    -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 Morrow, Aug 9, 2008
    1. Advertisements

  3. Jensen

    Ben Bullock Guest

    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

  4. 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>:

    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. )

    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 J McClellan, Aug 9, 2008
  5. Jensen

    Jensen Guest

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

    magloca Guest

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

    magloca, Aug 9, 2008
  7. Jensen

    Ted Zlatanov Guest

    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 Zlatanov, Aug 11, 2008
  8. Jensen

    Ben Morrow Guest

    I think this post was missing a smiley :).

    Ben Morrow, Aug 11, 2008
  9. Jensen

    Ted Zlatanov Guest

    Ted Zlatanov, Aug 11, 2008
  10. Tad J McClellan, Aug 12, 2008
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.