Skip to content

Instantly share code, notes, and snippets.

@tfrisk-old
Created January 21, 2015 09:02
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 tfrisk-old/bc7c896798f9f54c5078 to your computer and use it in GitHub Desktop.
Save tfrisk-old/bc7c896798f9f54c5078 to your computer and use it in GitHub Desktop.
Find count of code lines in Clojure source files
# Find count of code lines in Clojure source files
cat file.clj | grep -e '^$' -e '^;' -v | wc -l
# How it works?
#
# cat file.clj view file with cat command
# | pipe the output of cat command to the following commands
# grep read the input (piped cat) and match it to given regular expressions
# -e '^$' look for line start (^) followed by immediate end ($) -> empty line
# -e '^;' look for line start (^) followed by Clojure comment mark (;)
# -v invert all the matches to get the actual code instead of comment or empty lines
# | wc -l pipe the matched lines to wc command to count the lines (-l) option
#
# The command prints out an integer of counted lines
#
# Here we assume the comments always start from the very beginning of the line.
# This can be fixed by rewriting the second expression as
#
# -e '^\s*;'
#
# Here we are using \s* as an optional whitespace match (\s) matched 0 or more times (*)
# The whitespace fixed version is this
cat file.clj | grep -e '^$' -e '^\s*;' -v | wc -l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment