Skip to content

Instantly share code, notes, and snippets.

@purpleidea
Created December 18, 2017 11:28
Show Gist options
  • Save purpleidea/613e7df8c6e7b742c631d69f38569198 to your computer and use it in GitHub Desktop.
Save purpleidea/613e7df8c6e7b742c631d69f38569198 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Simple script to count the number of $pattern in each file
# Copyright (C) 2013+ James Shubin
# Written by James Shubin <james@shubin.ca>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
PAT='X''X''X' # the search pattern
FILE='.ignore' # the ignore storage
B=`basename $0` # the called program
ALL=false # show empty entries
N_COLS=`tput cols`
if [ "$1" == '--help' ]; then
echo "$0 [[--all] PATTERN | --help]"
exit 1
fi
if [ "$1" == '--all' ]; then
ALL=true
shift 1
fi
if [ "$1" != '' ]; then # use $1 as pattern!
PAT="$1"
fi
if [ "$2" == '--all' ]; then # look here as bonus
ALL=true
fi
if [ -e "$FILE" ]; then
IGNORE=( `cat $FILE 2> /dev/null` ) # load in file array
else
IGNORE=( `cat '.gitignore' 2> /dev/null` ) # .gitignore as backup
fi
function in_array() # needle in haystack
{
local i
needle=$1
shift 1
[ -z "$1" ] && return `false` # is array() defined
for i in $*
do
[ "$i" = "$needle" ] && return `true`
[ "$i" = "$needle/" ] && return `true` # directories
done
return `false`
}
M=''
s=`echo "$N_COLS/2 - 11" | bc` # how many to add in
M="$M"'<directory>' # add the dirname in
for i in `seq 1 $s`; do # add the spacers in
M="$M"' '
done
M="$M"'<count>'
# till end of row...
s=`echo "$N_COLS - $s - 19" | bc` # how many to add in
for i in `seq 1 $s`; do # add the spacers in
M="$M"' '
done
save="\033[1m\033[44m$M\033[0m" # delay this writing
t=0
for d in *; do # loop the main loop
if [ "$d" == "$B" ]; then # skip my own entry!
continue
fi
in_array "$d" ${IGNORE[@]}
result=$?
if [ $result -eq 0 ]; then
continue
fi
n=`grep -r "$PAT" "$d" | wc -l` # number of triple X
if [ "$n" == '0' ]; then # skip over zero err
if ! $ALL; then # show zeros or not?
continue
fi
fi
t=`echo $t + $n | bc`
l=`echo -n "$d" | wc -m` # length of dir name
s=`echo "$N_COLS/2 - $l" | bc` # how many to add in
if [ "$save" != '' ]; then # echo if one exists
echo -e "$save"
save=''
fi
echo -n ' ' # add a prefix space
echo -ne "$d" # add the dirname in
for i in `seq 1 $s`; do # add the spacers in
echo -n ' '
done
if [ "$n" == '0' ]; then # colourize the zero
echo -e "\033[1m\033[42m0\033[0m"
else
echo "$n"
fi
done
if [ "$save" != '' ]; then # no bugs were found
echo -e "\033[1m\033[42mzarro matches of pattern: '$PAT' found.\033[0m"
exit 0
fi
M=''
s=`echo "$N_COLS/2 - 7" | bc` # how many to add in
M="$M"'<total>' # add the dirname in
for i in `seq 1 $s`; do # add the spacers in
M="$M"' '
done
M="$M""<$t>"
# till end of row...
s=`echo "$N_COLS - $s - 13" | bc` # how many to add in
for i in `seq 1 $s`; do # add the spacers in
M="$M"' '
done
echo -e "\033[1m\033[44m$M\033[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment