Problem formatting a find with sed output

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'></script> 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'></script>\ \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'></script>\ \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?

Answers


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/


Untested:

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

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.


Thanks for your answers! I did like this:

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

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

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

Need Your Help

Does GC collect Binding Mode OneTime?

c# wpf

I am wondering if Binding gets released/collected by GC when Binding Mode is OneTime?