Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > search/replace for consecutive blank lines

Reply
Thread Tools

search/replace for consecutive blank lines

 
 
sbk
Guest
Posts: n/a
 
      03-27-2005
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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      03-27-2005


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.

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-27-2005
"sbk" <(E-Mail Removed)> 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      03-27-2005
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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-28-2005
sbk <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
sbk
Guest
Posts: n/a
 
      03-28-2005
thank you for the tips on posting (__DATA__), on using perldoc , and
on the use of /sg ...

--sk

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printing two consecutive lines icebrakers2008@gmail.com Perl Misc 9 08-22-2008 04:37 PM
extract consecutive lines of data baptiste Auguié Ruby 8 11-25-2007 03:46 PM
extracting values from consecutive lines baptiste Auguié Ruby 15 10-18-2007 10:12 PM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
Finding consecutive lines Peter Perl Misc 7 11-06-2004 08:06 AM



Advertisments