Skip to content

Instantly share code, notes, and snippets.

@dbrgn
Created September 7, 2011 13:48
Show Gist options
  • Save dbrgn/1200608 to your computer and use it in GitHub Desktop.
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
#!/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
$ 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