Best way to do a find/replace in several files?

I’ll throw in another example for folks using ag, The Silver Searcher to do find/replace operations on multiple files.

Complete example:

ag -l "search string" | xargs sed -i '' -e 's/from/to/g'

If we break this down, what we get is:

# returns a list of files containing matching string
ag -l "search string"

Next, we have:

# consume the list of piped files and prepare to run foregoing command
# for each file delimited by newline
xargs

Finally, the string replacement command:

# -i '' means edit files in place and the '' means do not create a backup
# -e 's/from/to/g' specifies the command to run, in this case,
# global, search and replace

sed -i '' -e 's/from/to/g'

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)