Newbie: Replacing double newlines

J

Jensen

Hello:

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?

Thanks.
 
B

Ben Morrow

Quoth Jensen said:
Hello:

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

-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
 
B

Ben Bullock

Hello:

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.

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

Tad J McClellan

Jensen said:
Hello:

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.


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


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

What gives?


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
 
J

Jensen

Ben said:
-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.

Thank you very much, that did it.
 
T

Ted Zlatanov

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
 
B

Ben Morrow

Quoth Ted Zlatanov said:
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).

I think this post was missing a smiley :).

Ben
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top