Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Please help with search/replace Perl string!

Reply
Thread Tools

Please help with search/replace Perl string!

 
 
Ron M.
Guest
Posts: n/a
 
      01-05-2004
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.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      01-05-2004

http://www.velocityreviews.com/forums/(E-Mail Removed) (Ron M.) wrote:
> 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

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
/Alcestis/) [ flame, and falls out of sight. ] (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      01-05-2004
(E-Mail Removed) (Ron M.) writes:

> 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\\
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      01-05-2004
"Brian McCauley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> (E-Mail 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



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-05-2004
Ron M. <(E-Mail Removed)> wrote:


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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
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
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments