Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

count total of comment lines in the file

Posted on 1998-07-14
19
Medium Priority
?
332 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
  • 8
  • 5
  • 3
  • +1
19 Comments
 
LVL 85

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 200 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 85

Expert Comment

by:ozo
ID: 2008793
main(){ /* How many comment lines in this program? */ /*
hahfhahhasdhk
hahfhahhasdhk
hahfhahhasdhk
hahfhahhasdhk
*/
printf("\"/*hahfhahhasdhk*/\\");
}
0
Technology Partners: 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 85

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 85

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 85

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 85

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 85

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 85

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

Technology Partners: 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!

Question has a verified solution.

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

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

916 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