75 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#!/usr/bin/env zsh
 | 
						|
 | 
						|
Help()
 | 
						|
{
 | 
						|
    echo "fif -g \"string\" -f \"regex pattern\" [-d \"path\"]      "
 | 
						|
    echo "    Options:                                              "
 | 
						|
    echo "        -g string        [REQUIRED] search string for grep"
 | 
						|
    echo "        -f regex         [REQUIRED] filemask for search   "
 | 
						|
    echo "        [-d directory]   path to search [defaults to . ]  "
 | 
						|
    echo "        -v               prints matching lines in files.  "
 | 
						|
    echo "                         (default is file list only)      "
 | 
						|
    echo "        -h               display this help                "
 | 
						|
    echo "                                                          "
 | 
						|
    echo "EXAMPLES:                                                 "
 | 
						|
    echo " fif -g \"hello\" -f \"(?i)^.*\.bas+$\"                   "
 | 
						|
    echo "    searches all files maching the mask '*.bas' in the    "
 | 
						|
    echo "    current working directory for the string 'hello'.     "
 | 
						|
    echo "                                                          "
 | 
						|
    echo " fif -g \"len\" -f \"(?i)^.*\.py+$\" -d /home/gmgauthier/Projects/code/python -v"
 | 
						|
    echo "    searches all files maching the mask '*.py' in the     "
 | 
						|
    echo "    'src/package' directory, for the string '.alen'       "
 | 
						|
    echo "    and prints the matched lines to the console           "
 | 
						|
    echo "                                                          "
 | 
						|
    echo "NOTES:                                                    "
 | 
						|
    echo "  - something similar can be done with a single grep.     "
 | 
						|
    echo "    eg: 'grep -rnw ~/Projects/code/python/passwdtools -e '.*\.length.*'"
 | 
						|
    echo "    However, the raw grep doesn't give you the            "
 | 
						|
    echo "    ability to choose which files to scan.                "
 | 
						|
    echo ""
 | 
						|
    echo "  - grep takes a simple search string                     "
 | 
						|
    echo "  - the filemask can be either ERE or PCRE regex          "
 | 
						|
    echo "  - all searches are recursive                            "
 | 
						|
    exit 0
 | 
						|
}
 | 
						|
 | 
						|
while getopts g:f:d:hv flag
 | 
						|
do
 | 
						|
    case "${flag}" in
 | 
						|
        g) grepstr=${OPTARG};;
 | 
						|
        f) filemask=${OPTARG};;
 | 
						|
        d) dir=${OPTARG};;
 | 
						|
        v) verbose=true;;
 | 
						|
        h) Help;;
 | 
						|
    esac
 | 
						|
done
 | 
						|
 | 
						|
if [[ -z $grepstr ]] then
 | 
						|
    echo "You must supply a search string in ere regex format"
 | 
						|
    exit 0
 | 
						|
fi
 | 
						|
 | 
						|
if [[ -z $filemask ]] then
 | 
						|
    echo "You must supply a filename filter for searches in pcre regex format"
 | 
						|
    exit 0
 | 
						|
fi
 | 
						|
 | 
						|
if [[ -z $dir ]] then
 | 
						|
    dir="."
 | 
						|
fi
 | 
						|
 | 
						|
# We have to validate the filelist first, otherwise grep will just 
 | 
						|
# try to search on whatever is in the PWD
 | 
						|
filelist=$(fd -0 $filemask $dir|sed 's/.$//')
 | 
						|
if [[ -z $filelist ]] || [[ $filelist == "" ]] then
 | 
						|
    echo "No files found matching your filemask. Search cannot be executed"
 | 
						|
    exit 1
 | 
						|
else
 | 
						|
    if [[ $verbose == true ]]; then
 | 
						|
        grep -inwrE $grepstr $(fd -0 -H $filemask $dir|sed 's/.$//') # sed strips fd's "%"
 | 
						|
    else
 | 
						|
        grep -linwrE $grepstr $(fd -0 -H $filemask $dir|sed 's/.$//') # sed strips fd's "%"
 | 
						|
    fi
 | 
						|
fi
 | 
						|
exit 0
 |