Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Last active July 9, 2018 14:26
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pesterhazy/72f98f600313e1a9ce1036dffb7ca8b1 to your computer and use it in GitHub Desktop.
Fixed-string replace in multiple files. Change files in place
#!/usr/bin/env bash
set -euo pipefail
# Fixed-string replace in multiple files. Changes files in place
if [[ $# -lt 3 ]]; then
echo "Syntax: frpl <from-string> <to-string> <file1> [file2 file3 ....]"
exit 1
fi
export FIND="$1"
shift
export REPLACE="$1"
shift
exec perl -i -ne '$f=quotemeta($ENV{"FIND"}); $t=$ENV{"REPLACE"}; s/$f/$t/g; print' "$@"
@pesterhazy
Copy link
Author

pesterhazy commented Jul 9, 2018

To use from git, add this to your ~/.gitconfig

[alias]
	frpl = "!f() { [ \"$GIT_PREFIX\" != \"\" ] && cd \"$GIT_PREFIX\"; git ls-files -z | xargs -0 frpl \"$@\" ; } ; f"

Then you can use:

git frpl <FROM> <TO>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment