Skip to content

Instantly share code, notes, and snippets.

@gongzhitaao
Last active December 25, 2015 13:19
Show Gist options
  • Save gongzhitaao/6982409 to your computer and use it in GitHub Desktop.
Save gongzhitaao/6982409 to your computer and use it in GitHub Desktop.
1. Count lines of code (LOC),, excluding blank lines and comments 2. Count number of methods in a class
#!/bin/bash
# usage: ./clsfunccnt <directory> <filename-regex> <classname>
find $1 -regex "$2" -exec cat {} \; | \
sed -n "
# find target class
/^class\s\+$3:/,/^class.*/{
# print all def's with 4 spaces indent
/\s\{4\}def\s\+.*/p
}" | wc -l
#!/bin/bash
# usage: ./wordcnt <directory> <filename-regex> [classname]
ARGC=("$#")
if [ $ARGC -eq 3 ]; then
find $1 -type f -regex "$2" -exec cat {} \; | \
sed -n "
# find the target class
/\(^class\s\+$3\b.*\)/,/^class\s\+.*/{
# magically removes the last line from pattern
0,/^class\s\+$3\b.*/!{
/^class\s\+.*/d
}
# remove blank line
/^\s*$/d
# remove comment lines starting with '#'
/^\s*#.*/d
# remove one line comment
/^\s*\"\"\".*\"\"\"/d
# remove multiline comment
/^\s*\"\"\"/,/^\s*\"\"\"/d
# print out the filtered result
p
}" | wc -l
else
find $1 -type f -regex "$2" -exec cat {} \; | \
sed -n '
/^\s*$/d
/^\s*""".*"""/d
/^\s*"""/,/^\s*"""/d
p
' | wc -l
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment