Perl multiline command syntax

N

Nigel

Hello

I am trying to use perl to find and replace in a large text file but I
cannot seem to get it to search over multiple lines.

# cat file.txt | perl -p - i -e
's/pattern(.*)endofpattern/replacement/g'

I have tried many combinations but whatever I try, I can only get it
to search line by line. I have read everywhere about using /m but i
dont understand where to use it.

Can someone please help me.
 
P

Paul Lalli

Hello

I am trying to use perl to find and replace in a large text file but I
cannot seem to get it to search over multiple lines.

# cat file.txt | perl -p - i -e
's/pattern(.*)endofpattern/replacement/g'

I have tried many combinations but whatever I try, I can only get it
to search line by line. I have read everywhere about using /m but i
dont understand where to use it.

Can someone please help me.

First, you need to tell Perl to read the whole file in as one string,
rather than line by line. Otherwise, your s/// is only operating on one
line at a time. To do this, supply the -0777 switch (see perldoc perlrun
for more info here).

Second, you need to tell the search-and-replace that . should match *any*
character, including a newline. By default, . does not match newlines.
To do this, provide the /s switch to the regexp. (The /m switch has
nothing to do with this issue - it's used to change the $ and ^ anchors,
which aren't used here).

Third, why are you bothering to invoke a cat process, rather than just
feeding the relevant file to the perl oneliner?


perl -0777 -pi -e 's/pattern.*endofpattern/replacement/gs' file.txt

(note that I removed the parens because $1 wasn't being used. If your
actual replacement does use $1, add the parens back in.)

Hope this helps,
Paul Lalli
 
T

Todd

Nigel said:
Hello

I am trying to use perl to find and replace in a large text file but I
cannot seem to get it to search over multiple lines.

# cat file.txt | perl -p - i -e
's/pattern(.*)endofpattern/replacement/g'

I have tried many combinations but whatever I try, I can only get it
to search line by line. I have read everywhere about using /m but i
dont understand where to use it.

Can someone please help me.
This works for me. It makes a backup copy and fixes the file.

perl -i.old -p -e "s/old_string/new_string/g" file.txt

Todd
 
J

John J. Trammell

I am trying to use perl to find and replace in a large text file but I
cannot seem to get it to search over multiple lines.

# cat file.txt | perl -p - i -e
's/pattern(.*)endofpattern/replacement/g'

I think this is explained well in the "perlrun" manpage; look at the
documentation for the "-p" and "-0" options. (That's a zero, not an
"oh".)

Two more comments:
* you want "-i", not "- i"
* you don't need to use "cat"--you can do this (untested):

perl -pi -0777 -e 'whatever' file.txt
 
T

Thomas Kratz

Nigel said:
Hello

I am trying to use perl to find and replace in a large text file but I
cannot seem to get it to search over multiple lines.

# cat file.txt | perl -p - i -e
's/pattern(.*)endofpattern/replacement/g'

I have tried many combinations but whatever I try, I can only get it
to search line by line. I have read everywhere about using /m but i
dont understand where to use it.

the /m modifier won't help here because it changes the behaviour of the
anchors ^ and $. You want /s because it lets . match a newline.

Also your pattern will only match once because the (.*) will eat up all
characters between the first occurence of 'pattern' and the *last*
occurence of 'endofpattern'. You have to switch to non-greedy matching by
putting a '?' after the quantifier.

So try:
's/pattern(.*?)endofpattern/replacement/sg'

And then reread 'perldoc perlre' again, and again, .... ;-)
You'll need more than a few rereads before you get familiar with it (I
sure did).

Thomas
 
P

Paul Lalli

This works for me. It makes a backup copy and fixes the file.

perl -i.old -p -e "s/old_string/new_string/g" file.txt

No it doesn't. At least, it doesn't do what the OP asked for. The OP was
specifically asking about making a search-and-replace where the pattern
spans multiple lines. Your code does not do that. See other replies in
this thread.

Paul Lalli
 
T

Tad McClellan

Nigel said:
I am trying to use perl to find and replace in a large text file but I
cannot seem to get it to search over multiple lines.


Searching over multiple lines in not the problem.

Getting multiple lines to search over in the first place is the problem.

# cat file.txt | perl -p - i -e


-p reads only one line at a time.

I have tried many combinations


Try this one:

perldoc -q matching

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

I have read everywhere about using /m but i
dont understand where to use it.


Use it when you want ^ and $ to mean "begin/end of line" instead
of meaning "begin/end of string".
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top