Skip to content

Instantly share code, notes, and snippets.

@wangmengbo
Forked from cef62/ag-replace.sh
Last active October 24, 2018 12:09
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 wangmengbo/aa738a4d3906b9dbcd58f3a129a5e21f to your computer and use it in GitHub Desktop.
Save wangmengbo/aa738a4d3906b9dbcd58f3a129a5e21f to your computer and use it in GitHub Desktop.
Search and replace using the silver searcher and sed
#!/usr/bin/env zsh
# Currently the script will choke on tilde characters used in either search or
# replace terms
# inspired by
# https://gist.github.com/hlissner/db74d23fc00bed81ff62
# https://gist.github.com/kates/5915285
# ag
# -0 --> Separate the filenames with \0, rather than \n: this allows
# xargs -0 <command> to correctly process filenames containing spaces
# or newlines.
# --files-with-matches --> Only print the names of files containing matches,
# not the matching lines. An empty query will print all files that
# would be searched.
# xargs
# -0 --> see ag description
# sed
# -r --> Use extended regular expressions rather than basic regular expressions
# -i replace in place, if followed by a suffix will create backup files
# -e --> execute the following command on any processed file
if [ $# -eq 3 ]; then
ag -0 --files-with-matches $1 $3 | xargs -0 -I {} sed -i '' -e "s~$1~$2~g" {}
elif [ $# -eq 4 ]; then
ag -0 --files-with-matches $1 $3 | xargs -0 -I {} sed -i.$4 -e "s~$1~$2~g" {}
elif [ $# -eq 2 ]; then
ag -0 --files-with-matches $1 | xargs -0 -I {} sed -i '' -e "s~$1~$2~g" {}
else
echo 'usage: $ ag-replace pattern_to_be_replacec pattern_to_replace_with /path/to/file(s)/for/searching [BACKUP_EXTENSION]'
echo 'eg: $ ag-replace red green src'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment