Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern matching/regular expression question 1

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US
How would I write something in Unix or Perl to change sentences or paragraphs in many web page documents. All the documents have some
similiar sentences and paragraphs. On occasion I need to change or
delete a specific sentence or paragraph. Any suggestions?
 
you can use find and perl. find will search the files
you want. perl will change their contents.

for instance, suppose you want to change all
paragraphs &quot;<p>this is a paragraph&quot; to
&quot;<p>this is another paragraph&quot; on all your
html files under your current directory:

$ find . -name \*.html |
xargs perl -i -p -e 'BEGIN{undef $/} s/<p>this is a paragraph/<p>this is another paragraph/sg'

now it's a question of tunning the regular expression
inside s///. most of the times you will want to ignore
spaces, then you do:
s/<p>\s*this is ...\s*/<p>\n new ... \n/sg
also, you might want to change something inside your
tags (some attribute, for example):
s/(<img[^>]*src=&quot;)x.gif(&quot;.*?>)/$1y.gif$2/sg
(the $2 part wasn't really necessary, but makes it
more understandable)

cya

--
pkiller


 
Thanks pkiller, that worked but I want to get an interactive script. I tried this:

#!/bin/ksh
echo enter data1
read data1
echo enter data2
read data2
find . -name p2 |
xargs perl -i -p -e 'BEGIN{undef $/} s/$data1/$data2/sg'


This doesnt change the data in file &quot;p2&quot;. Any suggestions?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top