- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- help me with search & replace string
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2012 06:23 AM
06-14-2012 06:23 AM
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
- Tags:
- quoting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2012 06:43 AM
06-14-2012 06:43 AM
Re: help me with search & replace string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2012 07:57 PM - edited 06-14-2012 07:57 PM
06-14-2012 07:57 PM - edited 06-14-2012 07:57 PM
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2012 11:33 PM
06-14-2012 11:33 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2012 11:49 PM
06-14-2012 11:49 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2012 11:56 PM
06-14-2012 11:56 PM
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?
file="/xxx/www/lll.xml"
file.new="/xxx/www/zzz.xml"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2012 12:13 AM
06-15-2012 12:13 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2012 12:23 AM
06-15-2012 12:23 AM
Re: help me with search & replace string (quoting)
${file} > ${file}.new
part of the script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2012 12:43 AM
06-15-2012 12:43 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2012 01:12 AM
06-15-2012 01:12 AM
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