Solved

counting lines of code using grep | wc -l

Posted on 2004-09-20
4
4,353 Views
Last Modified: 2008-02-01
Hi,

I want to count the number of lines of code in my code base.

I am thinking of doing something like:

grep –R “;” * | wc –l

except that counts all files (and I only want .h and .cpp).

How do I recursively count the number of lines in my code (roughly using the semi-colon)?

Thanks. =)

-Edward
0
Comment
Question by:edwardt
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 500 total points
ID: 12106764
You could use the 'find' command to filter the files found.
You could also cheat a bit with this (matches the filename
extension on the filename that grep prepends to the lines):

grep -H –R “;” * | grep -E "\.((h)|(cpp)):" | wc –l



0
 

Expert Comment

by:capncrunch_321
ID: 12109310
grep -c ';' *.h -r; grep -c ';' *.cpp -r
0
 
LVL 20

Expert Comment

by:Gns
ID: 12110383
No capncrunch_321 that won't work. The -r will work for the glob pattern you give... And at least I am not in the habit of naming my source directories foo.cpp;-).
bretmjonsons "el cheapo" grep relies on some flags that not all incarnation of grep has... (The -R was introduced between 2.4.2 and 2.5.1 version of GNU grep, so safer to use the --longstyle options:-)...
If one wants a version that'll work on most any unix/linux one will have to look to the find|xargs grep|wc thing mentioned in part by brettmjohnson... Something like
find /path/to/sourcedir -name \*.cpp -print -o -name \*.h -print | xargs grep ';' | wc -l

-- Glenn
0
 
LVL 20

Expert Comment

by:Gns
ID: 12110406
.... And note that macros (defines, ifdefs etc), and complex multiline function defines/calls aren't counted correctly here, nor are any "commented" lines that happen to contain a ";" excluded.
Might be more correct to just count 'em all and perhaps do a simple estimate as to the "comment&whitespace density";-).

-- Glenn
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now