-
-
Save dburr/2aa1f26968c82f87f505 to your computer and use it in GitHub Desktop.
This file contains 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
function sgrep { | |
ARGS_WITH_OPTS=2 | |
FILE_LIST_NO_BINARIES="-Il" | |
DEFAULT_OPTS_WITH_FIND="-hn" | |
DEFAULT_OPTS_WITHOUT_FIND="-rn" | |
SKIP_DIRS_RAW='svn' | |
SKIP_EXTS_RAW='pyc pyo so o a tgz' | |
SKIP_FILES_RAW='tags' | |
SKIP_DIRS="`echo $SKIP_DIRS_RAW | sed -e 's_\(\w*\)_.*/\.\1_g' | sed -e 's_ _\\\|_g'`" | |
SKIP_EXTS="`echo $SKIP_EXTS_RAW | sed -e 's_\(\w*\)_.*\.\1_g' | sed -e 's_ _\\\|_g'`" | |
SKIP_FILES="`echo $SKIP_FILES_RAW | sed -e 's_\(\w*\)_.*/\1_g' | sed -e 's_ _\\\|_g'`" | |
which grin &> /dev/null | |
HAVE_GRIN=`test $? == 0 && echo true || echo false` | |
grep --exclude-dir=x x /dev/null &> /dev/null | |
HAVE_EXCLUDE_DIRS=`test $? == 1 && echo true || echo false` | |
if $HAVE_GRIN; then | |
grin $* | |
elif $HAVE_EXCLUDE_DIRS; then | |
grep --exclude="$SKIP_FILES_RAW" --exclude-dir=."$SKIP_DIRS_RAW" $DEFAULT_OPTS_WITHOUT_FIND -r $* . | |
else | |
if [[ $# -le $ARGS_WITH_OPTS ]]; then | |
searchterm=$* | |
opts=$DEFAULT_OPTS_WITH_FIND | |
else | |
opts=shift | |
searchterm=$* | |
fi | |
find . \ | |
-regex "$SKIP_DIRS" -prune , \ | |
-not -regex "$SKIP_EXTS" \ | |
-not -regex "$SKIP_FILES" \ | |
-type f -print0 \ | |
| xargs -0 grep $FILE_LIST_NO_BINARIES "$searchterm" \ | |
| while read f; do echo "$f:"; grep --color=always $opts "$searchterm" "$f"; done | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment