Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
Get HideMyAss! VPN, PC Mag's Top 10 VPNs of 2016 for 55% off for a Limited Time ×
Unix

Journal karniv0re's Journal: Lines of Code Counter 2

Here's a quick little script that counts all the lines of code in my ColdFusion projects.

It can easily be adjusted to count other files.

#!/bin/bash

# Count lines of code starting at the parent directory

xmlfiles=`find . | grep "\.xml$"`;
cfmfiles=`find . | grep "\.cfm$"`;
cfcfiles=`find . | grep "\.cfc$"`;
jsfils=`find . | grep "\.js$"`;

total='0';

for file in $xmlfiles
do
total="$total + `wc -l $file | awk {'print $1'}`";
done

for file in $cfmfiles
do
total="$total + `wc -l $file | awk {'print $1'}`";
done

for file in $cfcfiles
do
total="$total + `wc -l $file | awk {'print $1'}`";
done

for file in $jsfiles
do
total="$total + `wc -l $file | awk {'print $1'}`";
done

echo $total | bc -q

This discussion has been archived. No new comments can be posted.

Lines of Code Counter

Comments Filter:
  • try total=$((total + `wc -l $file | awk {'print $1'}`))

    Other things:
    instead of grep try
    find . -name '*.xml'
    or even
    find . -name '*.xml' -o -name '*.cfm' -o -name '*.cfc' -o ...

    If you know that there's fewer than ~5000 files (bash's maximum # of arguments on a command line, compile-time option so YMMV) then you can pull it off in one line even:
    total=$(wc -l $(find . -name '*.xml' -o -name '*.cfm' -o -name '*.cfc' -o -name '*.js') | awk '/total$/ {print $1}')

    • Thanks Qzukk! I had originally tried bash arithmetic, but I was getting syntax errors, so I went with what I knew. :-) The find arguments are interesting as well. I imagine it's a little speedier than grepping through all the results. Your comments are appreciated!

Do not meddle in the affairs of troff, for it is subtle and quick to anger.

Working...