Last active
August 23, 2018 21:37
-
-
Save DrSnowbird/fa98181def1d1f2173b26c9a13b5c723 to your computer and use it in GitHub Desktop.
To search and replace for any matched pattern in the current directory including all the sub-directory.
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
#!/bin/bash -x | |
## Note: this script has dependency on "ack" utility. | |
if [ $# -lt 2 ]; then | |
echo "--------- Synopsis--------" | |
echo "Synopsis: Globally replace an old pattern withe a new text string for all directories given" | |
echo "Usage: $0 <old_pattern> <new_text> [directory, default: ./]" | |
echo "e.g." | |
echo " $0 '=logging\/' '/root/logging_files\/' /home/ec2-user/all/ansible/roles" | |
echo " " | |
exit 1 | |
fi | |
#### variables #### | |
OLD=$1 | |
NEW=$2 | |
DIR=${3:-./} | |
if [ ! -d "$DIR" ]; then | |
echo "*** ERROR: target directory not found!!" | |
exit 1 | |
fi | |
test_isMacOS=0 | |
function isMacOS() { | |
tmp=`uname -o 2>&1 |grep Darwin` | |
if [ ! "$tmp" == "" ]; then | |
test_isMacOS=1 | |
else | |
test_isMacOS=0 | |
fi | |
} | |
isMacOS | |
cd $DIR | |
files=`ack "$OLD" -l` | |
CURR_DIR=`pwd` | |
for f in $files | |
do | |
echo "-----------" | |
subDir=$(dirname $f) | |
newFile=$(basename $f) | |
cd ${subDir} | |
echo "... Process file: $DIR/$f ..." | |
echo "... into subdirectory: $subDir, to process file: $newFile" | |
## sed -i "s#=logging/#=/root/logging_files/#g" kibana-server-install.yml | |
if [ $test_isMacOS -eq 1 ]; then | |
sed -i '' "s#${OLD}#${NEW}#g" ./${newFile} | |
else | |
sed -i "s#${OLD}#${NEW}#g" ./${newFile} | |
fi | |
cd ${CURR_DIR} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment