Jump to content

string replacement script [solved]


Recommended Posts

I'm looking for a way to make a simple shell script that will search a file for string x and replace it with string y. Basically, a find-and-replace fuction that can be run from the command-line. Is there any easy way of doing this?

 

 

[moved from Software by spinynorman]

Link to comment
Share on other sites

having problems with that for two reasons. First, I need something that'll pick up parts of words. Like, I'm running this on an HTML document, and it seems that if I put in, say, href, it won't do anything because it's always href=. Also, since I'm running it on HTML, I need some way to put in slashes and other odd characters. I also need to find the command to output to a file...I read the man page and stuff, but couldn't find anything that worked. Any ideas? Here's the exact command I'm running:

 

sed s/script/test/ index.html

Link to comment
Share on other sites

sed writes to standard output and leaves your original file intact. So you have to redirect output to a new file. To create a new file with the changes, using your example:

 

sed 's/script/test/g' index.html > index_new.html

 

The /g is a global option to make changes to every occurrence on a line, not just the first.

 

Another handy command is 'tr' to translate certain non-printable ascii characters into other characters. Say you wanted to convert all tabs to spaces with the above conversion. Then you would pipe the output of the sed command through tr before redirecting output to the new file. You would use the octal value of the ascii character (see http://www.lookuptables.com/ for what the octal values are for ascii characters).

 

sed 's/script/test/g' index.html | tr '\011' ' ' > index_new.html

 

or alternately

 

sed 's/script/test/g' index.html | tr '\011' '\040' > index_new.html

Link to comment
Share on other sites

But how would I have it, say, replace the entire </script> tag? Because I can't put the braces or the slash into sed. I was thinking maybe use tr then run sed then run tr again....but....then I have to find a symbol I can have tr set them to that won't exist on it's own anywhere in the document....

Link to comment
Share on other sites

doesn't work...

 

[urza9814@Arochone ~]$ sed s/\<\/script\>/\(script\)/g index.html > indexnew.html

sed: -e expression #1, char 13: unknown option to `s'

[urza9814@Arochone ~]$ sed s/script/script/g index.html > indexnew.html

[urza9814@Arochone ~]$

Link to comment
Share on other sites

You need quotes around the expression. This works for me:

sed "s/<\/script>/(kipper)/g" index.html

You should only need a slash to escape the forward slash in </script>. The slashes to escape the round brackets are not necessary, the ones to escape the angle brackets < and > stop it from working.

 

Your original question about href is puzzling too - here it works just fine. If you have the line

<a href="something.html">blah</a>

and replace "href" with "kipper" then you should get

<a kipper="something.html">blah</a>

It shouldn't care about word boundaries unless you tell it to.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...