Skip to content

Instantly share code, notes, and snippets.

@someburner
Forked from hlissner/replace.sh
Last active March 5, 2020 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save someburner/d88c481e7ec903f7e4c4ec95397404d4 to your computer and use it in GitHub Desktop.
Save someburner/d88c481e7ec903f7e4c4ec95397404d4 to your computer and use it in GitHub Desktop.
Bulk search & replace with ag (the_silver_searcher)
# agr <search> <replace>
# place in .bashrc
function agr { ag -0 -l "$1" | AGR_FROM="$1" AGR_TO="$2" xargs -r0 perl -pi -e 's/$ENV{AGR_FROM}/$ENV{AGR_TO}/g'; }
export -f agr
# ORIGINAL
# ag <https://github.com/ggreer/the_silver_searcher>
# usage: ag-replace.sh [search] [replace]
# caveats: will choke if either arguments contain a forward slash
# notes: will back up changed files to *.bak files
ag -0 -l $1 | xargs -0 perl -pi.bak -e "s/$1/$2/g"
# or if you prefer sed's regex syntax:
ag -0 -l $1 | xargs -0 sed -ri.bak -e "s/$1/$2/g"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment