Last active
July 19, 2020 09:25
-
-
Save dhaigh/148117df0deddee5d824e3cf21fd4bc8 to your computer and use it in GitHub Desktop.
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 | |
# Usage: | |
# ./crunch.sh | |
FILE_LIST=/tmp/dirless_file_list | |
rg --files | grep -v '.lock$' > $FILE_LIST | |
TOTAL_FILES=$(cat $FILE_LIST | wc -l) | |
TOTAL_LINES=$(cat $FILE_LIST | tr '\n' '\0' | xargs -0 cat | wc -l) | |
file_percentage () { echo "$(echo "scale=1; 100 * $1 / $TOTAL_FILES" | bc)%"; } | |
line_percentage () { echo "$(echo "scale=1; 100 * $1 / $TOTAL_LINES" | bc)%"; } | |
divide () { echo "scale=1; $1 / $2" | bc; } | |
format_number () { printf "%'d" $1; } | |
crunch () { | |
files=$(cat $FILE_LIST | grep $1 $2 | wc -l) | |
echo ' files:' $(format_number $files) "($(file_percentage $files))" | |
lines=$(cat $FILE_LIST | grep $1 $2 | tr '\n' '\0' | xargs -0 cat | wc -l) | |
echo -n ' lines:' $(format_number $lines) "($(line_percentage $lines))" | |
echo " (avg $(divide $lines $files) per file)" | |
} | |
####################################################################################################################### | |
# CONFIGURE THE DIFFERENT SECTIONS YOU WANT HERE | |
sections=('\.css$' '\.html$' '\.js$' '\.py$' '\.csv$' '\.json$' '\.txt$' '\.sql') | |
####################################################################################################################### | |
for pattern in ${sections[@]}; do | |
echo ___________________________________________ | |
echo $pattern | |
crunch $pattern | |
done | |
echo =========================================== | |
echo NONE OF THE ABOVE PATTERNS | |
for i in ${!sections[@]}; do | |
# modify the array to wrap each pattern in parens (a bit dodgy) | |
sections[$i]="\(${sections[$i]}\)" | |
done | |
join_by () { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; } | |
crunch $(join_by '\|' ${sections[@]}) -v | |
# print all the files that didn't match any of the section patterns with this | |
# cat $FILE_LIST | grep -v $(join_by '\|' ${sections[@]}) | |
echo =========================================== | |
echo OVERALL | |
crunch '.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment