search/replace for consecutive blank lines

S

sbk

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
 
B

Brian McCauley

sbk said:
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.
 
A

A. Sinan Unur

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
 
J

John W. Krahn

sbk said:
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
 
T

Tad McClellan

sbk said:
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.
 
S

sbk

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

--sk
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top