D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] Multi-file replace



Neil Williams wrote:
> 
> There must be an easy way to do this . . . .
> 
> I have >500 files where an absolute reference href="http://www needs to be
> changed to a relative reference: href="../
> 
> The string always exists on a single line, and only once in each and every
> file.
> 
> How can this be automated? 

Being Linux probably a thousand different ways, and thats only
in perl.

"sed" is your friend.

This changes all occurence of fred to barney in test

sed -e "s/fred/barney/" test >test2  

for afile in *.html
do
 sed -e "s/fred/barney/" $afile >$afile.new
 ;; test return code and stuff
 mv $afile.new $afile
done

Would do all files in the current directory ending in html

Now we need ^ for start of line, and to protect the " and /
characters.....

So the sed ends up like...... assuming you wanted to zap the "."
after the www as well, which needs protecting if it is only dots
to match.

sed -e "s/^href=\"http:\/\/www\./href=\"\.\//" test

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.


Lynx friendly