-
-
Save shukebeta/76057fdeab4da5517232c8ec430f435e to your computer and use it in GitHub Desktop.
one script supports both grep and substitute. download this file and save it to your bin dir then run `ln -s ~/bin/g ~/bin/s`
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 | |
#set -ex | |
pname=`basename $0` | |
if [ $pname = "g" -a $# -lt 1 ];then | |
echo "Usage: $pname 'keyword' [-i]" | |
exit | |
fi | |
if [ $pname = "s" -a $# -lt 3 ];then | |
echo "Usage: $pname sourcestring targetstring path" | |
exit | |
fi | |
exclude_str="--exclude-dir=node_modules --exclude-dir=dist --exclude-dir=\.idea --exclude-dir=\.git --exclude-dir=vendor --exclude-dir=public --exclude=*ttf --exclude=*map --exclude=*gif --exclude=*jpg --exclude=*gdf --exclude-dir=MathJax --exclude-dir=PHPExcel --exclude-dir=phpQuery --exclude-dir=PHPQRCode --exclude-dir=node_modules --exclude-dir=data --exclude-dir=database --exclude-dir=Zend --exclude-dir=min --exclude-dir=css --exclude-dir=test " | |
filter_str=" | grep -v 'Binary file ' | grep -v '\(lib\|\.min\)\.js' | grep -v custom\.css | grep -v main_style\.css | grep -v template_build | grep -v .*-min\.js | grep -v 'storage/debugbar'" | |
if [ $pname = "g" ]; then | |
cmd="grep $exclude_str --color=always -nre '$1' . $2 $filter_str" | |
bash -c "$cmd" | |
else | |
filelistcmd="grep $exclude_str -rl '$1' '$3' $filter_str" | |
if [[ "$OSTYPE" == "darwin"* ]]; then | |
cmd="sed -i '' \"s/$1/$2/g\" \`$filelistcmd\`" | |
else | |
cmd="sed -i \"s/$1/$2/g\" \`$filelistcmd\`" | |
fi | |
bash -c "$cmd" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment