Created
September 7, 2011 13:48
-
-
Save dbrgn/1200608 to your computer and use it in GitHub Desktop.
A bash script to grep files in a specified directory and to replace values using 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
#!/bin/bash | |
##################################################################################### | |
# grep_and_replace.sh # | |
# # | |
# Usage: grep_and_replace.sh "grep-value" "sed-value" "directory" # | |
# Example: grep_and_replace.sh "old_path\/" "s/old_path\//new_path\//g" "~/project" # | |
##################################################################################### | |
COMMAND="egrep -lrZ --binary-files=without-match \"$1\" \"$PWD/$3\" | xargs -0 -l sed -i -e \"$2\"" | |
echo "Command: $COMMAND" | |
read -p "Really execute (y/n)? " | |
[ "$REPLY" != "y" ] || eval $COMMAND |
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
$ git status | |
# On branch master | |
nothing to commit (working directory clean) | |
$ find_replace.sh "TODO" "s/TODO/REMEMBER/g" apps/ | |
Command: egrep -lrZ --binary-files=without-match "TODO" "/home/user/apps/" | xargs -0 -l sed -i -e "s/TODO/REMEMBER/g" | |
Really execute (y/n)? y | |
[danilo@dev5 db]$ git status | |
# On branch master | |
# Changes not staged for commit: | |
# (use "git add <file>..." to update what will be committed) | |
# (use "git checkout -- <file>..." to discard changes in working directory) | |
# | |
# modified: apps/pagination.py | |
# modified: apps/folder1/performance_test.py | |
# modified: apps/folder1/models.py | |
# modified: apps/folder1/folder2/validation.py | |
# modified: apps/folder1/folder2/views.py | |
# | |
no changes added to commit (use "git add" and/or "git commit -a") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment