Skip to content

Instantly share code, notes, and snippets.

@cls
Created March 25, 2018 18:29
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 cls/87cf0f1219be89b60af5ffcde8ce54c9 to your computer and use it in GitHub Desktop.
Save cls/87cf0f1219be89b60af5ffcde8ce54c9 to your computer and use it in GitHub Desktop.
Execute sed substitution expression over files in a git tree
#!/bin/sh
expr="$1"
shift
pattern="$(printf "%s" "$expr" | sed -n 's/^s\/\(\([^\\\/]\|\\.\)*\)\/\([^\\\/]\|\\.\)*\/g\?$/\1/p')"
if [ -z "$pattern" ]
then
printf "usage: git sed s/lhs/rhs/[g] [file...]\n" > /dev/stderr
exit 2
fi
git grep -I -l -z "$pattern" "$@" | xargs -0 sed -i "$expr"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment