Last active
August 29, 2015 13:56
-
-
Save quickshiftin/8922723 to your computer and use it in GitHub Desktop.
Simple git find-&-replace utility (for use w/ GNU sed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-------------------------------------------------------- | |
# Simple search and replace utility for git repositories. | |
# @note Use w/ GNU sed. | |
# (c) Nathan Nobbe 2014 | |
# [email protected] | |
# http://quickshiftin.com | |
# $1 - Search | |
# $2 - Replace | |
# $3 - Subdirectory (optional, defaults to cwd) | |
#-------------------------------------------------------- | |
function git_find_replace | |
{ | |
local grepexp="$1" | |
local replace="$2" | |
local dir='.' | |
if [ ! -z "$3" ]; then | |
dir="$3" | |
fi | |
# Build sed command | |
local sedcmd="s/$grepexp/$replace/g" | |
# First find the files; then pipe through xargs sed for replacement | |
echo git grep --name-only "$grepexp" "$dir" '| xargs sed -i -r' "$sedcmd" | |
git grep --name-only "$grepexp" "$dir" | xargs sed -r "$sedcmd" -i | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this w/ BSD sed, simply change the
-i
component to-i ''
. You must explicitly supply an empty string to the-i
flag under BSD sed.