Solved

counting lines of code using grep | wc -l

Posted on 2004-09-20
4
4,406 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 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

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 …
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.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

623 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