Monday, August 21, 2006

Linux : find and replace with grep and sed

Following command finds PATTERN and replaces it with REPLACEMENT in all files under directory/path/.

grep -rl PATTERN directory/path/ | xargs sed -i 's/PATTERN/REPLACEMENT/g'

Notes :

-r option of grep does recursive search
-i option of sed edits files in place
g does substitutions in all occurences

1 comment:

Anonymous said...

-l option of grep outputs only the name of the file where a pattern is found.