Created
October 26, 2012 14:16
-
-
Save saltnlight5/3959069 to your computer and use it in GitHub Desktop.
replace.sh
This file contains hidden or 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
#!/usr/bin/env bash | |
# | |
# String replace on a file, or all files in a directory | |
# Author: Zemian Deng 10/26/2012 | |
# | |
# Usage: | |
# ./replace.sh my_file localhost `hostname` | |
# ./replace.sh my_dir localhost `hostname` | |
# DRY=1 ./replace.sh my_dir localhost `hostname` | |
# | |
DIR='.' | |
FROM='localhost' | |
TO=`hostname` | |
if [[ $# -ge 1 ]]; then DIR=$1; fi | |
if [[ $# -ge 2 ]]; then FROM=$2; fi | |
if [[ $# -ge 3 ]]; then TO=$3; fi | |
if [[ -d $DIR ]]; then | |
echo "Replacing all files in dir $DIR from $FROM to $TO" | |
echo find $DIR -type f -exec perl -i -pe "s/$FROM/$TO/g" {} \; | |
if [[ "$DRY" != "" ]]; then | |
echo "Dry run only." | |
else | |
find $DIR -type f -exec perl -i -pe "s/$FROM/$TO/g" {} \; | |
fi | |
else | |
echo "Replacing file $DIR from $FROM to $TO" | |
echo perl -i -pe "s/$FROM/$TO/g" $DIR | |
if [[ "$DRY" != "" ]]; then | |
echo "Dry run only." | |
else | |
perl -i -pe "s/$FROM/$TO/g" $DIR | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment