Skip to content

Instantly share code, notes, and snippets.

@thebirk
Last active October 2, 2019 04:51
Show Gist options
  • Save thebirk/9a2b9a6164f2006a3a5b4f4440f301bc to your computer and use it in GitHub Desktop.
Save thebirk/9a2b9a6164f2006a3a5b4f4440f301bc to your computer and use it in GitHub Desktop.
#/bin/bash
# clos - count lines of semicolons
# A simple utility that counts the number of semicolons in the supplied files
# NOT the number of lines with semicolons as the name implies, sorry about that
## MIT License
##
## Copyright (c) 2019 Aleksander B. Birkeland
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
if [[ $# -eq "0" ]]; then
echo "Usage: $0 <file>..."
exit
fi
semicolons=0
table=""
table="file|count"
for file in $@; do
if ! test -e "$file"; then
echo "$0: $file: No such file or directory"
fi
if ! test -f "$file"; then
continue
fi
lines=$(grep -o ";" $file | wc -l)
((semicolons += lines))
entry="\n$file|$lines"
table="$table$entry"
done
table="$table|\n"
table="${table}total|$semicolons\n"
echo -e $table | column -t -s "|"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment