Created
June 21, 2021 12:58
-
-
Save nickboldt/39629494e8035bf979c13e00cbb200d5 to your computer and use it in GitHub Desktop.
find.sh
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
#!/bin/bash | |
# find in files | |
if [[ $# -eq 0 ]]; then | |
echo "usage: $0 \"<dir(s)>\" \"<include filename pattern (*test*/*.java)>\" \"<searchstring(s)>\" \"<exclude filename pattern (target|.svn|.git)>\" [\"-mindepth 3 -maxdepth 4\"] [-quiet]"; echo ""; | |
echo "example: $0 . \"*test*/feature.*\" \"label|featureName\" target \"-mindepth 2 -maxdepth 4\" -q"; echo ""; | |
exit 1; | |
fi | |
if [[ $1 ]]; then dirs=$1; else dirs="."; fi | |
includepath="*" | |
if [[ $2 ]]; then | |
includepattern=$2; | |
if [[ ${includepattern##*/*} == "" ]]; then # includes path and filename, so split | |
includepath=${includepattern%%/*}; | |
includepattern=${includepattern##*/} | |
#echo "Got: $includepath AND $includepattern" | |
fi | |
else | |
includepattern="*"; | |
fi | |
searchstring=$3; | |
excludepattern="\.git/|/target/|\.jar|\.zip|\.class|\.png|node_modules|bower_components|svg" | |
if [[ $4 ]]; then | |
if [[ $4 == "|"* ]]; then excludepattern="${excludepattern}${4}" | |
elif [[ $4 == *"|" ]]; then excludepattern="${4}${excludepattern}" | |
else excludepattern="${4}" | |
fi | |
fi | |
echo ""; echo "Exclude: ${excludepattern}"; echo "" | |
minmax="$5" | |
option=$6; | |
if [[ "$option" = "-q" ]]; then echo; echo -n "st "; fi | |
cnt=0; | |
for d in $dirs; do | |
tmp=$(mktemp -p /tmp XXXXXX); rm -f $tmp; tmp="_"${tmp//\//}${tmp//\//}tmp"_"; # echo $tmp; # make random string (YY6Qjj), then change to _tmpYY6QjjtmpYY6Qjjtmp_ | |
if [[ $excludepattern != "" ]]; then | |
files=$(find $d $minmax -type f -name "$includepattern" | sort | egrep "$includepath" | egrep -v "$excludepattern" | sed -e "s/ /$tmp/g"); | |
else | |
files=$(find $d $minmax -type f -name "$includepattern" | sort | egrep "$includepath" | sed -e "s/ /$tmp/g"); | |
fi | |
# echo $files; # get files and apply temp string replacement for " " to keep files w/ spaces in names together | |
for f in $files; do | |
g=${f//$tmp/ }; # echo "$g ..."; # remove temp string replacement for " " | |
if [[ "$(egrep -c "$searchstring" "$g")" != "0" ]]; then | |
(( cnt++ )); | |
if [[ "$option" = "-q" ]] || [[ "$option" = "-quiet" ]]; then | |
echo -n "$f "; | |
else | |
echo "** [$cnt] $f ** "; | |
egrep -n -B2 -A2 "$searchstring" "$g"; | |
echo ""; | |
fi | |
fi | |
done | |
done | |
if [[ "$option" = "-q" ]]; then echo; echo; fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment