[GRASS-dev] batch search and replace from command line?

Glynn Clements glynn at gclements.plus.com
Wed Jan 9 14:14:38 EST 2008


Michael Barton wrote:

> I'm betting there is a Unix command to do a batch search and replace  
> of one string with another in all text files in a directory.
> 
> But I don't know what it is.

GNU sed has the following extension:

       -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if extension supplied)

If you use that option, you can pass multiple filenames, and sed will
process each one separately, e.g.:

	sed -i -e 's/old/new/g' *.txt

Without the -i script, the source files will be concatenated and
processed as a single stream.

If you don't have GNU sed, you need a loop, e.g.:

	for file in *.txt ; do
	    sed 's/.../.../g' "$file" > "$file".tmp && mv -f "$file".tmp "$file"
	done

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list