Operating System - HP-UX
1752280 Members
5240 Online
108786 Solutions
New Discussion

help me with search & replace string

 
NDO
Super Advisor

help me with search & replace string

Hi all

Please can you help me with a script to check several files for the following string:

 

encoding=""

 and replace it with:

encoding="UTF-8"

 I did the following, :

#!/bin/sh
string1="encoding="""
string2="encoding="UTF-8"
sed 's/'"$string1"'/'"$string2"'/g'

 but does not work.

Please can you help

FR

13 REPLIES 13
Steven Schweda
Honored Contributor

Re: help me with search & replace string

 
Dennis Handly
Acclaimed Contributor

Re: help me with search & replace string (quoting)

>but does not work.

 

It would help if you could also show the error.  As Steven said, it seems the only problem is in quoting string1 & string2.

You should also remove the quote stuttering in the sed command:

sed "s/$string1/$string2/g"

NDO
Super Advisor

Re: help me with search & replace string (quoting)

Hi

 

It does work now, but only for one file and if I insert the file name after the "sed" command, like:

 

sed  "s/$string1/$string2/g" text2insert.txt > newfile.out

 

But I have 30000 files to modify, and I cannot change the filename.

 

FR

Dennis Handly
Acclaimed Contributor

Re: help me with search & replace string (quoting)

>I have 30000 files to modify, and I cannot change the filename.

 

That's what scripting is for.  To handle > 1 cases.

If you have gnu sed or perl there is a -i option for in place update.

Otherwise you need to do something like:

sed  "s/$string1/$string2/g" $file > $file.new

if [ $? -eq 0 ]; then

   mv $file.new $file

fi

NDO
Super Advisor

Re: help me with search & replace string (quoting)

I am still in the learning phases of scripting, sorry if my next question is a dumb one:

I presume I have to declare $file with the path where this xml files are located right?

file="/xxx/www/lll.xml"
file.new="/xxx/www/zzz.xml"
Dennis Handly
Acclaimed Contributor

Re: help me with search & replace string (quoting)

>I presume I have to declare $file with the path where this xml files are located right?

 

Yes but not file.new, that's not a variable.

If you want to be pedantic about it:

sed  "s/$string1/$string2/g" ${file} > ${file}.new

NDO
Super Advisor

Re: help me with search & replace string (quoting)

my biggest problem is really to understand the

${file} > ${file}.new

part of the script
Dennis Handly
Acclaimed Contributor

Re: help me with search & replace string (quoting)

>my biggest problem is really to understand the ${file} > ${file}.new part of the script

 

${file} needs to take on each of your 30,000 files you need to change.

NDO
Super Advisor

Re: help me with search & replace string (quoting)

 

Hi

 

This is the final script:

#!/bin/sh
file="/tmp/fr/danilo/"
string1="encoding="""
string2="encoding="UTF-8"
sed  "s/$string1/$string2/g" $file > $file.new

if [ $? -eq 0 ]; then

   mv $file.new $file

fi

 And this is the error I am having when I run:

 

mceldev[253]/tmp/fr/danilo #./mudar
./mudar[4]: Syntax error at line 5 : `"' is not matched.
mceldev[254]/tmp/fr/danilo #

 

Can you help

 

FR