Solved

count total of comment lines in the file

Posted on 1998-07-14
19
330 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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.:
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…

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