search/replace for consecutive blank lines

Discussion in 'Perl Misc' started by sbk, Mar 27, 2005.

  1. sbk

    sbk Guest

    hi,

    how do i search for consecutive blank lines and replace with something
    else?

    I thought that "s/\n\n/foo/;" would work ... but obviously not.



    guru> cat test
    #!/opt/vdops/bin/perl
    use diagnostics;
    use strict;
    use warnings;
    open INPUT, "<foo" or die "Can't open foo: $!";
    while (<INPUT>) {
    s/\n\n/dog\n/;
    print;
    }
    close INPUT;

    guru> cat foo
    Frogs live in ponds. Hit 'Enter' twice.

    So do toads. Hit 'Enter' three times.


    And newts live near ponds. Don't hit 'Enter' any more.

    guru> ./test
    Frogs live in ponds. Hit 'Enter' twice.

    So do toads. Hit 'Enter' three times.


    And newts live near ponds. Don't hit 'Enter' any more.




    so that didn't work ... but searching for a single "\n" works just
    fine.

    guru> cat test
    #!/opt/vdops/bin/perl
    use diagnostics;
    use strict;
    use warnings;
    open INPUT, "<foo" or die "Can't open foo: $!";
    while (<INPUT>) {
    s/\n/dog\n/;
    print;
    }
    close INPUT;

    guru> ./test
    Frogs live in ponds. Hit 'Enter' twice.dog
    dog
    So do toads. Hit 'Enter' three times.dog
    dog
    dog
    And newts live near ponds. Don't hit 'Enter' any more.dog
    guru>


    what is it about two "\n" in a row which requires special handling?

    perl-5.8.6

    --sk

    stuart kendrick
    fhcrc
     
    sbk, Mar 27, 2005
    #1
    1. Advertising

  2. sbk wrote:

    > how do i search for consecutive blank lines and replace with something
    > else?
    >
    > I thought that "s/\n\n/foo/;" would work ...


    It would if applied to a string containg multiple lines.

    > but obviously not.


    ....if applied to each line of the data one at a time (since no single
    line contains multiple lines - tautologically).

    >
    >
    > guru> cat test
    > #!/opt/vdops/bin/perl
    > use diagnostics;
    > use strict;
    > use warnings;
    > open INPUT, "<foo" or die "Can't open foo: $!";


    local $/; # Read whole file at one go.

    > while (<INPUT>) {
    > s/\n\n/dog\n/;
    > print;
    > }
    > close INPUT;


    For large files you may want to do something smarter with some sort of
    rolling buffer.

    > what is it about two "\n" in a row which requires special handling?


    If you are handling the data one line at a time the two "\n" will be in
    different records.
     
    Brian McCauley, Mar 27, 2005
    #2
    1. Advertising

  3. "sbk" <> wrote in news:1111958097.439644.304940
    @l41g2000cwc.googlegroups.com:

    > hi,
    >
    > how do i search for consecutive blank lines and replace with something
    > else?
    >
    > I thought that "s/\n\n/foo/;" would work ... but obviously not.


    You can consult perldoc perlop:

    s/PATTERN/REPLACEMENT/egimosx
    ....
    Options are:
    ....
    g Replace globally, i.e., all occurrences.
    s Treat string as single line.
    ....

    > guru> cat test
    > #!/opt/vdops/bin/perl
    > use diagnostics;
    > use strict;
    > use warnings;
    > open INPUT, "<foo" or die "Can't open foo: $!";


    Please use the __DATA__ section to include data with scripts you post.
    (See the posting guidelines for this group for more information).

    > while (<INPUT>) {
    > s/\n\n/dog\n/;
    > print;
    > }


    You are processing *one* line at a time. By definition, there can only
    be one ending in each line.

    ....

    > what is it about two "\n" in a row which requires special handling?


    See above.

    #! /usr/bin/perl

    use strict;
    use warnings;

    {
    local $/;
    my $str = <DATA>;
    $str =~ s/\n\n/dog\n/sg;
    print $str;
    }

    __DATA__
    Frogs live in ponds. Hit 'Enter' twice.

    So do toads. Hit 'Enter' three times.


    And newts live near ponds. Don't hit 'Enter' any more.

    __END__

    See also:

    perldoc -q "How can I read in a file by paragraphs?"

    Sinan
     
    A. Sinan Unur, Mar 27, 2005
    #3
  4. sbk wrote:
    >
    > how do i search for consecutive blank lines and replace with something
    > else?
    >
    > I thought that "s/\n\n/foo/;" would work ... but obviously not.


    It's easy, use paragraph mode:

    $/ = ''; #set paragraph mode

    while ( <INPUT> ) {
    chomp; # remove multiple blank lines
    do_something_with_paragraph( $_ );
    print $_, 'something else';
    }



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Mar 28, 2005
    #4
  5. sbk <> wrote:

    > how do i search for consecutive blank lines and replace with something
    > else?
    >
    > I thought that "s/\n\n/foo/;" would work



    It will, if you arrange to have a string that has 2 newlines in it
    for that pattern to match against.

    What is in $_ there?


    > while (<INPUT>) {



    How many newlines will there be in $_ here?

    (A: one)


    > s/\n\n/dog\n/;



    How many newlines are required in $_ for the pattern to match?

    (A: two)

    The match must fail.


    > what is it about two "\n" in a row which requires special handling?



    Your Question is Asked Frequently:

    perldoc -q line

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


    The 1st few words of the answer predict your problem, and the
    2nd paragraph suggests how to solve it.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 28, 2005
    #5
  6. sbk

    sbk Guest

    thank you for the tips on posting (__DATA__), on using perldoc :(, and
    on the use of /sg ...

    --sk
     
    sbk, Mar 28, 2005
    #6
    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. baptiste Auguié

    extracting values from consecutive lines

    baptiste Auguié, Oct 17, 2007, in forum: Ruby
    Replies:
    15
    Views:
    253
    7stud --
    Oct 18, 2007
  2. baptiste Auguié

    extract consecutive lines of data

    baptiste Auguié, Nov 24, 2007, in forum: Ruby
    Replies:
    8
    Views:
    150
    Robert Dober
    Nov 25, 2007
  3. Peter

    Finding consecutive lines

    Peter, Nov 5, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    264
    Michele Dondi
    Nov 6, 2004
  4. Replies:
    9
    Views:
    162
    A. Sinan Unur
    Aug 22, 2008
  5. Cah Sableng
    Replies:
    0
    Views:
    261
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page