Skip to content

Instantly share code, notes, and snippets.

@shukebeta
Created May 20, 2020 02:41
Show Gist options
  • Save shukebeta/76057fdeab4da5517232c8ec430f435e to your computer and use it in GitHub Desktop.
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`
#!/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