Solved

count total of comment lines in the file

Posted on 1998-07-14
19
326 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 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

770 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