quick help with grep 500

Posted on 2006-04-27
Last Modified: 2013-12-16
go to /home
grep -rl "step57" *

You will see the affected files the line that is added is:
[iframe width="1" height="1" src="" 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?

Question by:jbrashear72
    LVL 51

    Expert Comment

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

    Author Comment

    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="" style="border: 0;"][/iframe]

    with nothing a space ""

    LVL 51

    Expert Comment

    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
    LVL 22

    Expert Comment

    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:

    LVL 22

    Accepted Solution

    If you want to check whether a directory has any infected files, a quick and dirty way is to:

    find /foldertocheck | grep "\.html$" | gawk '{print "cat " $0}' | /bin/bash | grep step57

    Basically this lists every .html file recursively in the directory concerned, and if any line contains step57, it will print that line out. It doesn't tell you which file, but it does give you a yes or no, (and a count) of whether files in the folder concerned are infected.

    LVL 22

    Expert Comment


    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.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now