Please help with search/replace Perl string!

R

Ron M.

I do a lot of web page maintenance, which includes frequently updating
some old sites with large numbers of static, text-based pages in a
UNIX or AIX environment. I use several Perl "tools" in my work,
although I'm not a Perl programmer.

Task: use the following Perl code to replace all occurrences of the
string, "this is the old string" with "this is the new string" and
create backup files with the ".back" suffix. Do this with all files
whose filename contains the string, ".html."

perl -pi.back -e 's/this is the old string/this is the new string/g'
*.html*

Problem: if, in the actual file, the string, "this is the old string"
is broken over two lines, the Perl code won't work! It has to be all
on the same line, or the text won't be replaced, and I have to go back
through each file one at a time and manually replace it in vi, which
is kinda self-defeating.

If it's possible, can somebody please change this Perl string so it
will catch those broken strings?

(please reply in the newsgroup)

Thanks a HEAP,
Ron M.
 
B

Ben Morrow

Task: use the following Perl code to replace all occurrences of the
string, "this is the old string" with "this is the new string" and
create backup files with the ".back" suffix. Do this with all files
whose filename contains the string, ".html."

perl -pi.back -e 's/this is the old string/this is the new string/g'
*.html*

Problem: if, in the actual file, the string, "this is the old string"
is broken over two lines, the Perl code won't work! It has to be all
on the same line, or the text won't be replaced, and I have to go back
through each file one at a time and manually replace it in vi, which
is kinda self-defeating.

If it's possible, can somebody please change this Perl string so it
will catch those broken strings?

If strings will only be broken at spaces:
perl -0777pi.back \
-e's/this[ \n]is[ \n]the[ \n]old[ \n]string/this is the new string/g' \
*.html*

If you want to keep the newlines:

perl -0777pi.back \
-e's/this([ \n])is([ \n])the([ \n])old([ \n])string/this$1is$2the$3new$4string/g' \
*.html*

To allow (and preserve) any amount of space between words:

perl -0777pi.back \
-e's/this(\s+)is(\s+)the(\s+)old(\s+)string/this$1is$2the$3new$4string/g' \
*.html*

If the lines can break anywhere, and you don't need to keep the
breaks:

perl -0777pi.back \
-e'my $str = "this is the old string"' \
-e'my $re = join "\n?", split //, $str' \
-e's/$re/this is the new string/g' \
*.html*

All of these are untested.

Ben
 
B

Brian McCauley

Task: use the following Perl code to replace all occurrences of the
string, "this is the old string" with "this is the new string" and
create backup files with the ".back" suffix. Do this with all files
whose filename contains the string, ".html."

perl -pi.back -e 's/this is the old string/this is the new string/g'
*.html*

Problem: if, in the actual file, the string, "this is the old string"
is broken over two lines, the Perl code won't work! It has to be all
on the same line, or the text won't be replaced, and I have to go back
through each file one at a time and manually replace it in vi, which
is kinda self-defeating.

You can tell Perl to treat the whole file as a single string by
undefining $/. Obviously you don't want to do this for large files.

This will still not find the pattern split over new-line boundaries
because that wouldn't match the literal spaces. You need to say that
your target pattern can match arbitrary whitespace between words.

perl -pi.back -e 'BEGIN { undef $/ }; s/this\s+is\s+the\s+old\s+string/this is the new string/g'

ISTR there's a way to undef $/ using the -l switch but I can't find it
in perlrun.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
G

gnari

Brian McCauley said:
(e-mail address removed) (Ron M.) writes:
perl -pi.back -e 'BEGIN { undef $/ };
s/this\s+is\s+the\s+old\s+string/this is the new string/g'
ISTR there's a way to undef $/ using the -l switch but I can't find it
in perlrun.

under -0 ?

gnari
 
T

Tad McClellan

Task: use the following Perl code to replace all occurrences of the
string, "this is the old string" with "this is the new string" and
Problem: if, in the actual file, the string, "this is the old string"
is broken over two lines,


Then it does not meet the condition set out in the Task.

the Perl code won't work!


Poor specifications often lead to poor implementations.

It has to be all
on the same line, or the text won't be replaced,


It if is not all on the same line, then it is not the string
"this is the old string", it is something else.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top