Help: Delete a single carriage return in a file, but not a double carriage return?

Discussion in 'Perl Misc' started by Steve Anderson, Jun 20, 2004.

  1. I'd like to use perl to remove all single carriage returns, but leave all
    double carriage returns intact in a text file.

    For example, the following:

    foo bar baz
    baz bar foo baz
    baz

    baz foo bar
    baz

    should become:

    foo bar baz baz bar foo baz baz

    baz foo bar baz

    It sounds simple, but I haven't been able to figure out how to do this.
    Can anyone give me a hand?

    Thanks,

    Steve
    Steve Anderson, Jun 20, 2004
    #1
    1. Advertising

  2. Steve Anderson

    Ben Morrow Guest

    Quoth Steve Anderson <>:
    > I'd like to use perl to remove all single carriage returns, but leave all
    > double carriage returns intact in a text file.
    >
    > For example, the following:
    >
    > foo bar baz
    > baz bar foo baz
    > baz
    >
    > baz foo bar
    > baz
    >
    > should become:
    >
    > foo bar baz baz bar foo baz baz
    >
    > baz foo bar baz
    >
    > It sounds simple, but I haven't been able to figure out how to do this.
    > Can anyone give me a hand?


    #!/usr/bin/perl

    $/ = $\ = "\n\n";

    while (<>) {
    chomp;
    s/\n//g;
    print;
    }

    __END__

    Ben

    --
    All persons, living or dead, are entirely coincidental.
    Kurt Vonnegut
    Ben Morrow, Jun 20, 2004
    #2
    1. Advertising

  3. Steve Anderson

    Anno Siegel Guest

    Steve Anderson <> wrote in comp.lang.perl.misc:
    > I'd like to use perl to remove all single carriage returns, but leave all

    ^^^^^^
    > double carriage returns intact in a text file.
    >
    > For example, the following:
    >
    > foo bar baz
    > baz bar foo baz
    > baz
    >
    > baz foo bar
    > baz
    >
    > should become:
    >
    > foo bar baz baz bar foo baz baz
    >
    > baz foo bar baz
    >
    > It sounds simple, but I haven't been able to figure out how to do this.
    > Can anyone give me a hand?


    You say "remove", but from your example you want to replace isolated
    line feeds with blanks.

    You can use lookbehind and lookahead to make sure a line feed is neither
    preceded nor followed by another:

    s/(?<!\n)\n(?!\n)/ /g;

    Anno
    Anno Siegel, Jun 20, 2004
    #3
  4. On Sun, 20 Jun 2004 12:29:20 +0000, Anno Siegel wrote:

    > You say "remove", but from your example you want to replace isolated
    > line feeds with blanks.


    You're absolutely right, my text was wrong, my example was right. Thanks
    for seeing through it, and thanks for the solution.

    Steve
    Steve Anderson, Jun 21, 2004
    #4
    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. Randall Parker
    Replies:
    1
    Views:
    582
    S. Justin Gengo
    Dec 12, 2005
  2. Sydex
    Replies:
    12
    Views:
    6,425
    Victor Bazarov
    Feb 17, 2005
  3. Eric Layman
    Replies:
    3
    Views:
    605
    Rad [Visual C# MVP]
    Apr 14, 2007
  4. X X
    Replies:
    4
    Views:
    336
    red floyd
    Jul 19, 2010
  5. Xeno Campanoli
    Replies:
    0
    Views:
    205
    Xeno Campanoli
    Feb 13, 2006
Loading...

Share This Page