How to mass Search & Replace in text files.

William O'Higgins Witteman william.ohiggins-H217xnMUJC0sA/PxXw9srA at public.gmane.org
Fri May 1 14:41:15 UTC 2009


On Fri, May 01, 2009 at 10:26:16AM -0400, Lance F. Squire wrote:
> One of the sites on my server has been infected with some malicious Java  
> Script.
>
> I can locate all the files with:
>
> grep "Bad JS" `find . -name "*.html"`
>
> Is there a way to modify this to replace the "Bad JS" with ""?
>
> Or a find variant?

There are lots of ways, and I suspect you'll get a raft of good
suggestions.  I have never been good at using the shell, so I do things
like this with a programming language - mostly Python these days, but in
this case I have an old Perl script to do this.  Here it is:


#!/usr/bin/perl -p -i

# This is a teeny tiny little script that takes filenames or wildcards
# as its argument, opens that/those files, and performs the regex on
# them.  It is too customized to have a guide, but as I change things
# I'll preserve the previous incarnation to give me a more comprehensive
# set of examples.

# This regex looks for image tags and excises them completely - it will
# barf on an escaped greater-than in a comment or filename, because it
# uses a look-behind to complete the expression on the greater-than
# symbol.
#
######################################################################
#                                                                    #
# Remove image tags from html                                        #
#                                                                    #
######################################################################

#s/<img[^>]*>//gs;

######################################################################
#                                                                    #
# Convert ampersands to their HTML character entity.                 #
#                                                                    #
######################################################################

#s/&/&amp\;/gs;

######################################################################
#                                                                    #
# Turn strings starting with www into hyperlinks.                    #
#                                                                    #
######################################################################

#s/(www\.[\S]+)\b/<a href="http:\/\/$1">$1<\/a>/gs;

######################################################################
#                                                                    #
# Changing the keyword expansion tags to match use of subversion     #
#                                                                    #
######################################################################

s/# Current Revision: \$revision\$/# \$Id\$/gs;


# This is the end of the file, be careful with it, as it can potentially
# mess up a lot of files.  It does not recurse though, which limits its
# potential damage.
-- 

yours,

William

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://gtalug.org/pipermail/legacy/attachments/20090501/99fb2fc1/attachment.sig>


More information about the Legacy mailing list