Last active
January 29, 2021 04:29
-
-
Save jranson/7e0db868a8501c908b98dce6e5678670 to your computer and use it in GitHub Desktop.
deepgrep (recursive grep with globs)
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
# add this function to your .bash_profile or .zshrc | |
# | |
# usage (all files): deepgrep 'my[Expression]' | |
# usage (filtered files): deepgrep '*.go' MyString | |
# | |
# if your file filter uses a glob wildcard, enclose it in quotes | |
# | |
deepgrep() { | |
if [[ $# -eq 0 || $# -gt 2 ]] | |
then | |
print "deepgrep ['*.ext'] expression" | |
exit | |
fi | |
DGFF='*' | |
if [[ $# -eq 2 ]] | |
then | |
DGFF=$1 | |
fi | |
DGLF=${@: -1} | |
for file in $(find . -name $DGFF) ; do echo $file; grep -n "$DGLF" $file 2>&1; done | grep -v grep | grep "$DGLF" -B 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment