[kwlug-disc] Block level search and replace

Chris Frey cdfrey at foursquare.net
Wed Feb 23 17:13:15 EST 2011


On Wed, Feb 23, 2011 at 05:04:02PM -0500, Insurance Squared Inc. wrote:
> I've got an old script I can't quite get working.  What's the best way 
> to do a block search and replace across many files?  i.e. the text I'm 
> searching and replacing might be a bit longer than a small line.  like:
> find:
> <body><center>
> Replace with:
> <body><A href="http://www.example.com">Home</a> : <a 
> href="/link2">Link2</a> : <a href="/link3">Link 3</a>

If you can guarantee that <body><center> are on one line, you could try:

	sed 's,<body><center>,<body><A href="http://www.example.com">Home</a> : <a href="/link2">Link2</a> : <a href="/link3">Link 3</a>,' < filename1 > filename2

To do that for a bunch of files, wrap it in a for loop:

	mkdir /something/newset
	cd oldset
	for f in *.html ; do
		sed 'above expr' < "$f" > "/something/newset/$f"
	done

- Chris





More information about the kwlug-disc mailing list