Solved

count total of comment lines in the file

Posted on 1998-07-14
19
323 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now