Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

counting lines of code using grep | wc -l

Posted on 2004-09-20
4
Medium Priority
?
4,421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 2000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

715 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