Counting lines of code in a directory with awk

I've never toyed with awk, but it seems every time I present an elegant 2- to 8-line script, someone comes back with an awk 1-liner.

I just came up with this to count all the lines of source code in a directory. How would I do it in awk?

LINES=0
for n in $(wc -l *.cpp *.h | cut -b-7); do
    ((LINES += n))
done
echo $LINES
$ awk 'END {print NR}' *.cpp *.h
1 Like

Cool!

If anybody is trying this at home, note that I forgot to get rid of the 'total' line out of wc, so my script was reporting double.

---------- Post updated at 06:51 PM ---------- Previous update was at 06:49 PM ----------

Come to think of it, here is how I could have done it:

wc -l *.cpp *.h | tail -n1