Created
October 20, 2011 20:40
-
-
Save tmhedberg/1302315 to your computer and use it in GitHub Desktop.
Shell function to search specific files for a pattern (augmented grep)
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
| # Common pattern for searching file contents | |
| fxg () { | |
| local OPTIND=1 findbase=. grepflags verbose=false usage=false | |
| while getopts b:g:vh opt; do | |
| case $opt in | |
| b) findbase=$OPTARG ;; | |
| g) grepflags=$OPTARG ;; | |
| v) verbose=true ;; | |
| h) usage=true ;; | |
| esac | |
| done | |
| [[ -n $grepflags ]] && | |
| grepflags=-$grepflags | |
| shift $(($OPTIND - 1)) | |
| if [[ $# -ne 2 ]] || $usage; then | |
| cat >&2 <<'.' | |
| Usage: fxg [options] <find glob> <grep pattern> | |
| -b <base> - Base directory for search | |
| -g <flags> - Flags to be passed to grep | |
| -v - Provide verbose output | |
| -h - Print this usage summary | |
| . | |
| return | |
| fi | |
| if $verbose; then | |
| echo "finding $1 in $findbase containing \"$2\" with grep flags" \ | |
| "$grepflags" >&2 | |
| fi | |
| find "$findbase" -name "$1" | | |
| xargs grep $grepflags "$2" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment