Mass text replace in multiple files with Sed

I came across a neat method of replacing a string in loads ‘o’ files today.
Basically pipe the output of grep for your search string into sed doing a regex replace on the string.

grep -lre 'SEARCH_STRING' . | xargs -d'\n' sed -i "s/SEARCH_STRING/REPLACE_STRING/g"

2 Responses to “Mass text replace in multiple files with Sed”

  1. Davide says:

    …which is basically the same as doing

    find . -type f -exec grep -q ‘SEARCH_STRING’ -exec sed -i ’s/SEARCH_STRING/REPLACE_STRING/g’ {} \;

    …which in turn is the same as

    find . -type f -exec sed -i ’s/SEARCH_STRING/REPLACE_STRING/g’ {} +

  2. Mark says:

    Nice, I actually like the last one you said - shorter :)

Leave a Reply