Solved

count total of comment lines in the file

Posted on 1998-07-14
19
328 Views
Last Modified: 2010-04-21
Which command Can i use to count total of comment lines in the C code file.
the comment line pattern is /*hahfhahhasdhk*/

thanks
0
Comment
Question by:rongz
[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
  • 8
  • 5
  • 3
  • +1
19 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2008791
is
/*hahfhahhasdhk*/ printf("/*hahfhahhasdhk*/"); /*hahfhahhasdhk*/ one or two comment lines?

How about
/*hahfhahhasdhk
  hahfhahhasdhk
  hahfhahhasdhk
  hahfhahhasdhk*/
?
0
 
LVL 1

Accepted Solution

by:
hajek earned 50 total points
ID: 2008792
awk '   BEGIN { COUNT=0 }
     /\/\*/,/\*\// { COUNT++ }
    END { print COUNT }
    ' _the_file_

this gives you total number of lines where comments occur (including:
{
   call_xxx();   /* comment */
}
or
/* comment
comment
 end comment */

0
 
LVL 84

Expert Comment

by:ozo
ID: 2008793
main(){ /* How many comment lines in this program? */ /*
hahfhahhasdhk
hahfhahhasdhk
hahfhahhasdhk
hahfhahhasdhk
*/
printf("\"/*hahfhahhasdhk*/\\");
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rongz
ID: 2008794
I just tried hajek's solution. It failed. the output is total of lines in the file. In fact I want to know the total of code lines and total of comment lines in a C-coded file.



0
 
LVL 84

Expert Comment

by:ozo
ID: 2008795
Ok, what's a code line?
0
 

Author Comment

by:rongz
ID: 2008796
I just tried hajek's solution. It failed. the output is total of lines in the file. In fact I want to know the total of code lines and total of comment lines in a C-coded file.



0
 
LVL 1

Expert Comment

by:hajek
ID: 2008797
Maybe my solution does not do what you want, but it does NOT return total number of lines - I suppose you do not have comments on every line, do you ??

What EXACTLY you want ?

PS: when file is:
--------------------------------
{
      call_xxx();   /* comment */
}
/* comment
comment
comment */
--------------------------------
it returns 4, there are only 2 lines (of 6), which does not include kinda comment.


0
 

Author Comment

by:rongz
ID: 2008798
code line is all the lines except comment lines
0
 
LVL 84

Expert Comment

by:ozo
ID: 2008799
So is
 call_xxx();   /* comment */
a code line or a comment line?

BTW, I agree that hajek's solution doesn't work,
by any reasonable definition of comment line, unless you stick to  hajek's programming style,
but it also doesn't return total number of lines.
0
 

Author Comment

by:rongz
ID: 2008800
if file content is :
       
 call_xxx();   /* comment */


 Then      total of code line is 1
             total of  comment line is 1

0
 
LVL 1

Expert Comment

by:hajek
ID: 2008801
ozo: hey, man, what don't you like on my programming style ???
Too quick for you ?

OK, just kidding. I know it can return greater number, than it should to, but if rongz wanted to count lines which begin with "/*" and end with "*/", he was to write it clearly.

0
 
LVL 84

Expert Comment

by:ozo
ID: 2008802
so, is


a code line, if it's not a comment line?
is
   /* comment */;
a code line?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2008803
hajek, I didn't say I don't like your C programming style
(indeed, it makes comment counting easier than more pathological styles:-)
But if a comment counter is supposed to work on any C program,
I don't know if it can be counted on.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2008804
as rongoz said at his comment (July 14 1998 - 04:50AM PDT):

x=y; /* comment */ y=x; /* comment */; x /* comment */ =y;

is one code line *and* one comment line,
so a simple solution is:

awk '{lines++} /\/\*/&&/\*\// {comment++} END { print "code="lines", comments="comment}' _the_file_

NOTE: it fails for netsted comments across multiple lines
and if there are lines containing both literals "/*"  *and*  "*/"  anyhow (for example as strings)
0
 

Author Comment

by:rongz
ID: 2008805
Sorry hajek. I just tried your solution again.It's perfect.
also thanks ahoffmann.
BTW, grep -c also can count the total of lines matching pattern. But how can I write the pattern. The following command doesn't work.

grep -c  /\/\*/,/\*\// _the_file_    
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2008806
grep -c '/\*.*\*/' _the_file
0
 
LVL 84

Expert Comment

by:ozo
ID: 2008807
that counts
  printf("/*hahfhahhasdhk*/");
but not
/*
hahfhahhasdhk
*/
to count comments correctly requires something like

#!/usr/bin/perl
$/ = undef;
$_ = <>;
$c += !defined $2 while m#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.\|[^'\\])*'|\n+|.[^/"'\\]*)#g;
print $c;
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2008808
ozo, I know that (see my comments). Would you like to see some more obfuscating code:

printf("/*"); /* comment*/ char */*comment*/str="*/";

real compilers have no problems with such things ;-)

BTW, could you please explain where to find &lt and &gt in perl (which perl version)?


0
 
LVL 84

Expert Comment

by:ozo
ID: 2008809
The given perl script also has no problem with such things.

What do you mean by &lt and &gt ?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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 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.:

738 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