One-line global string replace in all files with sed (or awk?)

Anthony Atkielski atkielski.anthony at wanadoo.fr
Fri Jan 28 03:04:39 PST 2005


Giorgos Keramidas writes:

GK> grep will do.  You just have to pass it the right option:
GK>
GK>         find . -type f | xargs grep -l 'foo' | \
GK>             xargs sed -i '' -e 's/foo/bar/g'
GK>
GK> When passed the -l option (this is a lowercase 'EL'), it will not print
GK> the matched lines.  Only the name of the files that *do* match. Then,
GK> once you have a list of files that really do match with 'foo' as a
GK> pattern, you can xargs sed on the list to substitute whatever you want :-)

It's interesting that there are multiple ways to do this in FreeBSD,
whereas I've never found a way to do it in Windows, even with the fancy
Visual InterDev I've been using for several years (it will _search_ for
strings in multiple files, but it won't do replacements, so you have to
search and then edit each file by hand--try doing this several hundred
times, several times in a row sometime!).

-- 
Anthony




More information about the freebsd-questions mailing list