[kwlug disc.] find and replace

Chris Frey cdfrey at foursquare.net
Wed Jan 24 00:49:59 EST 2007


On Tue, Jan 23, 2007 at 11:36:04PM -0500, Insurance Squared Inc. wrote:
> Is there an easy way to do a command line search and replace with the 
> replace being a block of complicated text?  i.e I want to insert about 
> 10 lines of php code into all the files in a directory at a specific 
> spot in each file (after the <body> tag actually).  Using sed or perl -i 
> -p -e is clunky to say the least - too much escaping of characters.  
> Ideally a 'search, and replace with contents of this file', but I'm not 
> seeing how to do this.

awk perhaps?  I'm sure perl does this too, but awk is faster for me:

# for f in *.html ; do awk '
{print $0}
/<body>/{print "This is where you place your\
large and ungainly text insertion\
where all you need to escape are the double\
quotes, single quotes from the shell, and the newlines..."
}' < $f > $f.new
done

This places the new text after the line containing <body>.

- Chris



More information about the KWLUG-Disc mailing list