Need to remove the first N lines of grep result files

I am trying to get rid of a hacker problem on some of my wordpress installations.

This guy puts 9 lines of code in the head of several files on my server ... I am trying to use grep and sed to solve this problem.

I'm trying to:

grep -r -l "//360cdn.win/c.css" | xargs -0 sed -e '1,9d' < {} 

But nothing happens if I delete the result -0 from xargs , the result of the files found are clean, but they are not overwriting the origin file with the sed`, can someone help me with this?

Many thanks!

+6
source share
2 answers

You must use the --null option in the grep to output the NUL byte or \0 after each file name in the grep output. Also use -i.bak in sed for in-line editing of each file:

 grep -lR --null '//360cdn.win/c\.css' . | xargs -0 sed -i.bak '1,9d' 
+2
source

What is wrong with iterating over files directly?

And you can add -i flat to sed so that the files are edited in place

 grep -r -l "//360cdn.win/c.css" | while read f do sed -e '1,9d' -i "${f}" done 

¹ OK, you may have problems if your files contain newlines, etc. but then ... if your site contains files with newlines, you probably have other problems ...

0
source

Source: https://habr.com/ru/post/1013390/


All Articles