1753501 Members
5028 Online
108794 Solutions
New Discussion юеВ

sed search and replace

 
SOLVED
Go to solution
Jannik
Honored Contributor

sed search and replace

I have been looking for a solution for this, but can't find one.

I have a lot of .html files that i need to edit though crontab.

This is the string I am looking for:
src="*../something/styleAssets.somthing.something*"
src='*../something/styleAssets.somthing.something*'
href="*../something/styleAssets.somthing.something*"
href='*../something/styleAssets.somthing.something*'

to be replaced with a string:
http:/mydomain.com/styleAssets

I were thinking of something like this:

for i in $(find /www/myDir -type f -name '*.html')
do
sed -e 's/styleAssets/http:\/\/mydomain.com\/styleAssets/g' $i > $i.new
cp $i.nw $i
done

But that is not correct since i need to replace all within the single or double quote.

tnx.
jaton
13 REPLIES 13
OFC_EDM
Respected Contributor

Re: sed search and replace

It would help if you provided a line or 2 of the actual text which included the string(s) you want to replace.

The Devil is in the detail.
Steven E. Protter
Exalted Contributor

Re: sed search and replace

Shalom,

Try using escape characters.

http://archives.devshed.com/forums/unix-linux-135/sed-regular-expression-search-replace-help-373459.html

http://www.unix.com/shell-programming-scripting/44886-shell-script-find-replace-string-multiple-files.html

http://www.jonasblog.com/2006/05/search-and-replace-in-all-files-within-a-directory-recursively.html

I think one of the links will work for you.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Jannik
Honored Contributor

Re: sed search and replace

Hey Kevin

text:


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

menu











should bee changed to something like this.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

menu











And only if the reference hold the text styleAssets to begin with.

BR jt
jaton
Dennis Handly
Acclaimed Contributor

Re: sed search and replace

Try these:
sed -e 's;".*/styleAssets\..*\..*\..*";"http://mydomain.com/styleAssets";g' \
-e "s;'.*/styleAssets\\..*\\..*\\..*';'http://mydomain.com/styleAssets';g" \
$i
Jannik
Honored Contributor

Re: sed search and replace

I have some of the solution...

Input file:





Perl script href.pl:
s/href=(["'][^"']+["'])/href=\"http:\/\/mydomain.com\/styleAssets\"/ig

Perl script src.pl:
s/src=(["'][^"']+["'])/src=\"http:\/\/mydomain.com\/styleAssets\"/ig

The run script:
for i in $(find ./ -type f -name '*.html')
do
perl -p -i.old1 href.pl $i
perl -p -i.old2 scr.pl $i
done

But this solution also change lines witch does not hold a reference to styleAssets.

I need some "if" statement in the perl script.

jaton
James R. Ferguson
Acclaimed Contributor

Re: sed search and replace

Hi:

# perl -pi.old -e 's{((src|href)=")\.\..([^"]+")}{$1http:/mydomain.com/$2}' file

...which also preserves the original file as "file.old".

Regards!

...JRF...
Jannik
Honored Contributor

Re: sed search and replace

Hey Dennis your does not seem to work.
Hey James your perl works partly except that it changes everything between the quotes even if it does not hold the word styleAssets.

I'm trying to make the changes but it seems that I need some help with that.
jaton
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed search and replace

Hi (again) Jannik:

> Hey James your perl works partly except that it changes everything between the quotes even if it does not hold the word styleAssets.

Oops, sorry about that.

# perl -pi.old -e 's{((?:src|href)=")\.\.(/styleAssets[^"])}{$1http:/mydomain.com$2}' file

Regards!

...JRF...
Jannik
Honored Contributor

Re: sed search and replace

If have the string thanks James.

Perl file (file.pl):
s/(href|src)(=["']).+(tyleAssets)/\1\2http:\/\/mydomain.com\/s\3/ig

Run command:
for i in $(find ./ -type f -name '*.html')
do
perl -p -i.old file.pl $i
done

thanks!
jaton