Skip to content

Instantly share code, notes, and snippets.

Created February 16, 2011 22:40
Show Gist options
  • Save anonymous/830443 to your computer and use it in GitHub Desktop.
Save anonymous/830443 to your computer and use it in GitHub Desktop.
That's right, that's all there is to your mom.
#!/bin/bash
SKIP_DIRS_RAW='svn hg git bzr'
SKIP_EXTS_RAW='pyc pyo so o a tgz'
SKIP_FILES_RAW='.*/tags|.*/\.coverage'
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=$SKIP_FILES_RAW
# Do a case insensitive search
if [[ "$1" == "-i" ]]; then
shift
CASE_FLAG="-i"
fi
GREP="zgrep -s -E $CASE_FLAG"
TEST_GREP="$GREP -q"
INTERACTIVE_GREP="$GREP -n -C 3 --color=always"
# Should we display context or filenames only?
if [[ "$1" == "-l" ]]; then
shift
TO_RUN=print_files_only
else
TO_RUN=print_with_context
fi
function print_with_context
{
find . \
-regex "$SKIP_DIRS" -prune , \
-not -regex "$SKIP_EXTS" \
-not -regex "$SKIP_FILES" \
-type f \
-exec $TEST_GREP "$@" {} \; \
-printf "\033[1;33m%p\n\033[0m" \
-exec $INTERACTIVE_GREP "$@" {} \; \
| less -FRSX
}
function print_files_only
{
find . \
-regex "$SKIP_DIRS" -prune , \
-not -regex "$SKIP_EXTS" \
-not -regex "$SKIP_FILES" \
-type f \
-exec $TEST_GREP "$@" {} \; \
-print
}
$TO_RUN $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment