Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    using sed to delete numerous lines from files

    I am trying to use the sed command to delete spam from an old guest book system (very old) on one of my clients servers. Since the system is so old it did not have any type of captcha or anti-spam techniques other than some simple error checking for the fields. The guest book does not use any database, it simply adds the user's comments inside an html table and appends it to the end of the guestbook.html file right before the footer. This is one of those free homepage sites so there are thousands of guest books. I have come up with the following script that will delete posts that contain urls since most of the spam is simply a list of urls the spammer tried linking to.

    find httpdocs -type f -name guestbook\.html -exec sed -i \{\} -e '/\[URL/id' -e '/lt;a/id' -e '/.*http:\\\/\\\/[^<>\r\n]+[^>]/id' \; -exec echo \{\} \;

    Hopefully that doesn't get word wrapped, but the above should be all on one line. The above only deletes one line. Which works great during testing b/c the guest book replaces all new lines in a posters comments with html break tags "<BR>", which means all the comments always appear on one line no matter how long. Anyway the only existing problem is the table in which the comments were in still remains. To fix this all I need to do is delete the 6 lines above the comments and 3 lines below, since the postings are always the same number of lines. Also I realize this might be better solved with a php script but was hoping to keep it simple. And by the way, yes, they have updated their guest book system but these old guest books still remain.

    Any help would be greatly appreciated :)

  2. #2
    Can you use grep -v with the -B6 -A3 flags? I'll leave you to do the hard work of investigating and implementing! Obviously you'd have to dump to a temporary file and replace the original.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts