views:

1101

answers:

4

I have some 150 files and in them I want to remove this following code:

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="/height.js"></SCRIPT>

What I'm doing is:

sed -e 's/<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="/height.js"></SCRIPT>/ /' file_names

This doesn't seem to work.

I want to remove this script from all the files in one go. How can I do that?

+1  A: 

You need to escape the special characters with an extra backslash.

Note that the output will also all go to the console. If you want 150 separate output files, you might want to look at the xargs command, something like:

ls -1 | xargs -t -i 'sed -e -i "replace comment" {}'

Be aware that the sed '-i' option will edit the files in place so get your replacement right first and back the files up!

Jon
+1  A: 

You have to worry about the slashes in the text you are replacing.

  • Either: use '\/' for each slash,
  • Or: cheat and use '.' to match any character at the point where the slash should appear.

The alternative exploits the improbability of a file containing the HTML. Theoretically, if you don't like the second alternative, you should also use '\.' at each point where '.' appears in the string you're looking at.

sed -e 's/<SCRIPT LANGUAGE="JavaScript" TYPE="text.javascript" SRC=".height.js"><.SCRIPT>/ /' file_names

This is copied from your example and slashes are replaced by dots. However, supplying all the file names on the command line like that will simply write the output as the concatenation of all the edited files to standard output.

Classically, to edit files more or less in situ, you'd write:

tmp=${TMPDIR:-/tmp}/xxx.$$
trap 'rm -f $tmp; exit 1' 0 1 2 3 13 15
for file in ...list...
do
    sed -e '...' $file > $tmp
    mv $tmp $file
done
rm -f $tmp
trap 0

This includes reasonably bullet-proof clean-up of the temporary - it is not perfect. This variant backs up the original before replacing it with the edited version:

tmp=${TMPDIR:-/tmp}/xxx.$$
trap 'rm -f $tmp; exit 1' 0 1 2 3 13 15
for file in ...list...
do
    sed -e '...' $file > $tmp
    mv $file $file.bak
    mv $tmp $file
done
rm -f $tmp
trap 0

With GNU sed, you can use the '-i' or '--in-place' option to overwrite the files; you can use '--in-place=.bak' to create backup copies of each file in file.bak.

Jonathan Leffler
sed -e 's/\<SCRIPT LANGUAGE\=\"JavaScript\" TYPE\=\"text\/javascript\" SRC\=\"\/height.js\"\>\<\/SCRIPT\>/ / i used this code and it has blanked my file :-( –
developer
(a) You were testing on dummy copies of the files before going live with anything, weren't you? (b) You do not need to randomly scattershot backslashes in the expression - and you missed the backslash before the dot that might actually do some good. However, neither of those explains what happened. With sed run without the GNU-specific '-i' option, there is no way for sed itself to demolish your file; it works in read-only mode. So, there must have been a mistake in the surrounding shell script that you were playing with.
Jonathan Leffler
If you tried "`sed -e 's/.../ /' < file > file`" as suggested by someone else (I hope as a temporary aberration), then the shell clobbers your file for you automatically. And it doesn't matter what the command you use is - the shell clobbers the file before executing the command; it is empty before the command is executed.
Jonathan Leffler
heheh yaa obviously i wont use my original file for testing.....my file is pretty safe :-) ..........i used backslash with dot(.) as well but it still didnt worked....that code is as it is :-(
developer
@Jonathan : your code worked fine but the thing is that on shell screen it is showing the output without the script code but when i checked my files on FTP they were still containing that old script....why is it so??? when i have already ran that code to remove the script then why it still there in files??? im not very good in shell so plzz guide!!!
developer
By original design, sed reads a file (without modifying it) and writes the edited version to standard output. That's why you see it on the screen. That is also why you have to take care to redirect the output. You've not said which platform you are on (and therefore whether you have GNU sed). Assuming that the edit is working correctly now... If you have GNU sed (or MacOS or, by inference, BSD), then use the `-i` option to do the in situ editing (but be aware that MacOS requires a backup suffix). If not, use the modified version of the script in the answer that does backups. (in a mo')
Jonathan Leffler
A: 

You should pipe the output e.g.

sed -e 's/<.*SRC="\/height.js".*>//g' < foo.html > foo1.html
Tzury Bar Yochay
i tried but this is the error that it is showing :-sed: -e expression #1, char 24: unknown option to `s'
developer
Bad, bad, *bad*, **bad** I/O redirection - the output redirection clobbers the input file (empties it) before the command is executed, much less gets to read anything from it. You've just created an empty file.
Jonathan Leffler
I added the \ escape before the '/heightworks fine now
Tzury Bar Yochay
@Jonathan, you are right! fixed that
Tzury Bar Yochay
A: 

You don't have to use the / with the s command:

sed 's|old|new|' files...

If you want to do inline-replace, then the first step is to back up all of your files then issue this command, substitute 'old' and 'new' with strings of your choice:

sed -i .bak 's|old|new|' files...

Because inline replacement is very dangerous, I can't emphasize enough that you must back up all of your files before running the command.

Did I mention that you should back up all of your files?

Hai Vu