Counting lines of code in a directory with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting lines of code in a directory with awk
# 1  
Old 08-04-2010
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?
Code:
LINES=0
for n in $(wc -l *.cpp *.h | cut -b-7); do
    ((LINES += n))
done
echo $LINES

# 2  
Old 08-04-2010
Code:
$ awk 'END {print NR}' *.cpp *.h

This User Gave Thanks to Scott For This Post:
# 3  
Old 08-04-2010
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:
Code:
wc -l *.cpp *.h | tail -n1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting lines in a file using awk

I want to count lines of a file using AWK (only) and not in the END part like this awk 'END{print FNR}' because I want to use it. Does anyone know of a way? Thanks a lot. (7 Replies)
Discussion started by: guitarist684
7 Replies

2. Shell Programming and Scripting

sed&awk: replace lines with counting numbers

Dear board, (I am trying to post this the 3rd time, seems there's some conflicts with my firefox with this forum, now use IE) ------ yes, I have searched the forum, but seems my ? is too complicated. ------------origianl file --------------- \storage\qweq\ertert\ertert\3452\&234\test.rec... (4 Replies)
Discussion started by: oUo
4 Replies

3. UNIX for Dummies Questions & Answers

Counting # of lines

Counting number of lines: sp I am trying to figure out a script to count the number of text files in cywig and have it give me a number (as the answer) any help would be appreciated. I am new here, so be gentle :D (3 Replies)
Discussion started by: unicksjp
3 Replies

4. Shell Programming and Scripting

counting lines containing two column field values with awk

Hello everybody, I'm trying to count the number of consecutive lines in a text file which have two distinctive column field values. These lines may appear in several line blocks within the file, but I only want a single block to be counted. This was my first approach to tackle the problem (I'm... (6 Replies)
Discussion started by: origamisven
6 Replies

5. UNIX for Advanced & Expert Users

Counting files in a given directory

Hi all, Need some help counting files... :) I'm trying to count the number of files in a given directory (and subdirectories) which reportedly contains "thousands" of files. I'm using this: ls -R | wc -l However it's been an hour and looks like it's still running; there is no output... (18 Replies)
Discussion started by: verdepollo
18 Replies

6. Shell Programming and Scripting

Add 5 lines of code to all the scripts in a directory

Hi Guys, I need some tips on writing a Korn shell script that would look for certain lines of code and replace all the scripts in the directory with a few other lines. I have about 120 scripts that I need to modify. Any suggestions would be appreciated! Thanks, Cool_avi (5 Replies)
Discussion started by: coolavi
5 Replies

7. UNIX for Dummies Questions & Answers

Awk counting lines with field match

Hi, Im trying to create a script that reads throught every line in a file and then counts how many lines there with a certain field that matches a input, and also ausing another awk it has to do the same as the above but to then use sort anduniq to get rid of all the unique lines with another... (8 Replies)
Discussion started by: fredted40x
8 Replies

8. Shell Programming and Scripting

awk - Counting number of similar lines

Hi All I have the input file OMAK_11. OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE ... (8 Replies)
Discussion started by: dhanamurthy
8 Replies

9. Shell Programming and Scripting

Merging files with AWK filtering and counting lines

Hi there, I have a couple of files I need to merge. I can do a simple merge by concatenating them into one larger file. But then I need to filter the file to get a desired result. The output looks like this: TRNH 0000000010941 ORDH OADR OADR ORDL ENDT 1116399 000000003... (2 Replies)
Discussion started by: Meert
2 Replies

10. UNIX for Dummies Questions & Answers

Line counting in Directory

I want to count the no of lines for files (.c and .h) present in a directory structure. My code is: #!/bin/bash # Usage: linecount.sh directory_name for file in $(find $1 -name ); do wc -l "$file" >> filecount.txt done Problem is that the directory structure is really big... (3 Replies)
Discussion started by: MobileUser
3 Replies
Login or Register to Ask a Question