Skip to content

Instantly share code, notes, and snippets.

@markusfisch
Last active January 15, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markusfisch/94ae54b8d1911622c7df to your computer and use it in GitHub Desktop.
Save markusfisch/94ae54b8d1911622c7df to your computer and use it in GitHub Desktop.
Count indent levels and echo lines that are indented over a given limit
#!/usr/bin/env bash
# Read lines from stdin and echo them if the indent is too deep
#
# @param 1 - levels of indent to consider too much (optional)
# @param 2 - indent character (optional)
check_indents()
{
local THRESHOLD=${1:-3}
local INDENT=${2:-$'\t'}
local LINE=0
local HITS=0
while read -r
do
local COUNT=0
while [[ $REPLY == $INDENT* ]]
do
REPLY=${REPLY:1}
(( ++COUNT ))
done
if (( COUNT > THRESHOLD ))
then
printf "% 4d (%dx) %s\n" $LINE $COUNT "$REPLY"
(( ++HITS ))
fi
(( ++LINE ))
done
return $HITS
}
if [ "${BASH_SOURCE[0]}" == "$0" ]
then
check_indents "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment