1

I am trying to remove 2 lines from all my Javascript files on my Linux shared hosting. I wanted to do this without writing a script as I know this should be possible with sed. My current attempt looks like this:

find . -name "*.js" | xargs sed -i ";var 
O0l='=sTKpUG"

The second line is actually longer than this but is malicious code so I have not included it here. As you guessed my server has been hacked so I need to clean up all these JavaScript files.

I forgot to mention that the output I am getting at the moment is:

sed: -e expression #1, char 4: expected newer version of sed

The 2 lines are just as follows consecutively:

;var

O0l='=sTKpUG

except that the second line is longer, but the rest of the second line should not influence the command.

2
  • 1
    What does your sed command mean? I cannot understand it. Commented Mar 22, 2013 at 2:20
  • awk may be a better choice than sed in this case. I generally find awk easier to use when dealing with multiple-line type situations as you seem to have here. It will probably be a bit more verbose, but much more understandable. Commented Mar 28, 2013 at 0:36

2 Answers 2

2

He meant removing two adjacent lines.

you can do something like this, remember to backup your files.

find . -name "*.js" | xargs sed  -i -e "/^;var/N;/^;var\nO0l='=sTKpUG/d"

Since sed processes input file line by line, it does not store the newline '\n' character in its buffer, so we need to tell it by using flag /N to append the next line, with newline character.

/^;var/N;

Then we do our pattern searching and deleting.

/^;var\nO0l='=sTKpUG/d
Sign up to request clarification or add additional context in comments.

5 Comments

That is almost it except that the pattern you have used it not the same as the one I have and I still get an error: unterminated address regex
@user920840 oops, I edited the command, can you check if it works?
I am still getting an error - sed: -e expression #1, char 300: unknown command: `8'
Does the fact that the second line has around 1600 characters and that I am pasting this whole command from notepad to putty make any difference?
You don't have to paste the whole line in to the command, just put enough characters to make sure only lines you want to delete match the pattern. You can run the command directly, without caring about what the rest of lines are.
0

It really isn't clear yet what the two lines look like, and it isn't clear if they are adjacent to each other in the JavaScript, so we'll assume not. However, the answer is likely to be:

find . -name "*.js" |
xargs sed -i -e '/^distinctive-pattern1$/d' -e '/^alternative-pattern-2a$/d'

There are other ways of writing the sed script using a single command string; I prefer to use separate arguments for separate operations (it makes the script clearer).

Clearly, if you need to keep some of the information on one of the lines, you can use a search pattern adjusted as appropriate, and then do a substitute s/short-pattern// instead of d to remove the short section that must be removed. Similarly with the long line if that's relevant.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.