Urza9814 Posted September 1, 2006 Report Share Posted September 1, 2006 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] Quote Link to comment Share on other sites More sharing options...
scoonma Posted September 2, 2006 Report Share Posted September 2, 2006 Hi Urza, obviously you're looking for the shell command "sed". :-) Quote Link to comment Share on other sites More sharing options...
Urza9814 Posted September 2, 2006 Author Report Share Posted September 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
jboy Posted September 2, 2006 Report Share Posted September 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
Urza9814 Posted September 2, 2006 Author Report Share Posted September 2, 2006 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.... Quote Link to comment Share on other sites More sharing options...
paul Posted September 2, 2006 Report Share Posted September 2, 2006 s/\<\/script\>/\(script\)/g replace </script> with (script) Quote Link to comment Share on other sites More sharing options...
Urza9814 Posted September 2, 2006 Author Report Share Posted September 2, 2006 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 ~]$ Quote Link to comment Share on other sites More sharing options...
neddie Posted September 3, 2006 Report Share Posted September 3, 2006 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. Quote Link to comment Share on other sites More sharing options...
Urza9814 Posted September 3, 2006 Author Report Share Posted September 3, 2006 oh hey, that fixes everything...hah thanks :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.