help me with search & replace string (722 Views)
Reply
Super Advisor
NDO
Posts: 534
Registered: ‎12-10-2003
Message 1 of 14 (722 Views)

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

Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,084
Registered: ‎02-23-2005
Message 2 of 14 (715 Views)

Re: help me with search & replace string

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,943
Registered: ‎03-06-2006
Message 3 of 14 (698 Views)

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

[ Edited ]

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

Please use plain text.
Super Advisor
NDO
Posts: 534
Registered: ‎12-10-2003
Message 4 of 14 (691 Views)

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

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,943
Registered: ‎03-06-2006
Message 5 of 14 (683 Views)

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

Please use plain text.
Super Advisor
NDO
Posts: 534
Registered: ‎12-10-2003
Message 6 of 14 (677 Views)

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"
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,943
Registered: ‎03-06-2006
Message 7 of 14 (667 Views)

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

Please use plain text.
Super Advisor
NDO
Posts: 534
Registered: ‎12-10-2003
Message 8 of 14 (664 Views)

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

my biggest problem is really to understand the

${file} > ${file}.new

part of the script
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,943
Registered: ‎03-06-2006
Message 9 of 14 (657 Views)

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.

Please use plain text.
Super Advisor
NDO
Posts: 534
Registered: ‎12-10-2003
Message 10 of 14 (656 Views)

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

Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,084
Registered: ‎02-23-2005
Message 11 of 14 (649 Views)

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

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,943
Registered: ‎03-06-2006
Message 12 of 14 (636 Views)

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

[ Edited ]

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

 

This is the same error that Steven told you how to fix before and again just now.

 

>file="/tmp/fr/danilo/"

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

 

Your assignment to "file" above isn't going to work since will be treated as a directory path when adding ".new".

If you do want to scan/change mass quantities, you need to tell us how you find those 30,000 files.

Also, the above code changes the file modification date, even if the string isn't found.  Do you want to not change the dates on those?

 

>If you do get the quotation marks into string1 and string2, what do you think will happen when you add more quotation marks to them, as in: "s/$string1/$string2/g"

 

I'm not sure of your point?  If I got those double quotes into the strings (using single quotes), I had no problems with the sed command.  (At least with bash.  And a real shell works too.)

 

I wasn't sure so I explicitly checked it.  Reading the man page briefly may not provide enough detail to figure out what it does, so experiments are also helpful.

Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,084
Registered: ‎02-23-2005
Message 13 of 14 (626 Views)

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

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,943
Registered: ‎03-06-2006
Message 14 of 14 (622 Views)

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

>It works for me, too. My usual quotation paranoia must be getting out of control.

 

Yes, I didn't think it would be that easy either.

 

It appears once something is quoted, it doesn't matter what the variables inside expand to.

Or course programs like sed and grep may need even more quoting.  :-)

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation