Counting commented lines (full-line comments only) 
Author Message
 Counting commented lines (full-line comments only)

Hello,
            I want to count full-line comments in a program. In my case,
A comment is a line or a part of line which starts with "|" (similar to
// syntax in C++).  The following doesn't work sometimes properly :

    awk 'BEGIN {kount=0}                \
        /^[ \t]*\|.*/ { kount++ }       \
        END {print "count of comments = " kount } '  <filename>

    Also,
        grep -c "^[ \t]*\|" <filename>
    gives different output

    Can some one explain about this behaviour for same pattern ?

  DETAILS :
        My input could  be as follows :

        | compare values and do something ...
            if ... then    |* is x = y ?!
                ....
            endif
                        |* half-commentt .. .
                  |*  one more comment . . .
        |----------- functions -----------
                <more statements ....         >

        In this particular example, my output
        must be 4.

 Thanks in advance,
 Sridhar.



Sun, 01 Apr 2001 03:00:00 GMT  
 Counting commented lines (full-line comments only)
You could try

    awk 'BEGIN {kount=0}                \
        {if(substr($1,1,1)=="|") { kount++ }}       \
        END {print "count of comments = " kount } '  <filename>

Worked for me although obviously it is only applicable to finding full line
comments - you would need oto devise some other method to find comments at
the end of a line

Hope that helps,

Cesar



Sun, 01 Apr 2001 03:00:00 GMT  
 Counting commented lines (full-line comments only)

Quote:
>     awk 'BEGIN {kount=0}                \
>         /^[ \t]*\|.*/ { kount++ }       \
>         END {print "count of comments = " kount } '  <filename>

You shouldn't have to search for tabs explicitly as you are doing now.
In fact, the way your code is written, you must have a tab in order to
get a "hit". By default, awk uses tabs and spaces as field seperators
and so basically ignores them. Your code could be:
awk '/^\|/ {++kount}; END {print "count of comments = " kount}' <fname>
It is likewise unecessary to initialize variables in awk; they are zero
by default, although I recognize that many C programmers will do this
anyway. To count every line with a | character and therefore presumably
all comments in a file:
awk '/\|/ {++kount}; END {print "count of comments = " kount}' <fname>

--Erik



Sun, 01 Apr 2001 03:00:00 GMT  
 Counting commented lines (full-line comments only)

Quote:

> >   awk 'BEGIN {kount=0}                \
> >       /^[ \t]*\|.*/ { kount++ }       \
> >       END {print "count of comments = " kount } '  <filename>

> You shouldn't have to search for tabs explicitly as you are doing now.
> In fact, the way your code is written, you must have a tab in order to
> get a "hit".

Huh?! Sridhar's regular expression correctly matches a record (i.e.,
a line) that begins with zero or more space or tab characters followed
by a vertical bar. (His use of .* unnecessarily matches the rest of
the string and has no impact on the success or failure of the match.)
This regular expression does not require a tab anywhere in the record
for a successful match.

Quote:
> By default, awk uses tabs and spaces as field seperators and so
> basically ignores them.

This is false. An awk regular expression pattern never arbitrarily
ignores anything. The class of characters awk uses as the default
field separator is immaterial here as this script doesn't use fields
($1, $2, ..., $NF).

Quote:
> Your code could be:
> awk '/^\|/ {++kount}; END {print "count of comments = " kount}' <fname>

It could be, but it wouldn't be correct for his application. This script
counts only those lines in which the first character is a vertical bar.
Sridhar clearly defined what he wants to count:

     I want to count full-line comments in a program. In my case,
     a comment is a line or a part of line which starts with "|" (similar
     to // syntax in C++).

     [...]

  DETAILS:
        My input could be as follows :

        | compare values and do something ...
            if ... then    |* is x = y ?!
                ....
            endif
                        |* half-commentt .. .
                  |*  one more comment . . .
        |----------- functions -----------
                <more statements ....         >

        In this particular example, my output
        must be 4.

Quote:
> To count every line with a | character and therefore presumably
> all comments in a file:
> awk '/\|/ {++kount}; END {print "count of comments = " kount}' <fname>

This certainly counts all comments in a file, but it also incorrectly
counts each of the following lines:

     foobar ~ /foo|bar/
     if (foo || bar)
     print "a vertical bar: |"
     foo++  |* increment the foo counter

In the last line above, the vertical bar begins a comment, but it is not
a "full-line comment" and, as such, is not a line that Sridhar wants to
count. (Op. cit.)

--
Jim Monty

Tempe, Arizona USA



Sun, 01 Apr 2001 03:00:00 GMT  
 Counting commented lines (full-line comments only)

%             I want to count full-line comments in a program. In my case,
% A comment is a line or a part of line which starts with "|" (similar to
% // syntax in C++).  The following doesn't work sometimes properly :
%
%     awk 'BEGIN {kount=0}                \
%         /^[ \t]*\|.*/ { kount++ }       \
%         END {print "count of comments = " kount } '  <filename>

This looks fine.  For the sake of nit-picking, the .* is not needed, but
it doesn't hurt, either.  Could you give an example input file where it
fails?

You could also try
 awk 'BEGIN { count = 0 }
      $1 ~ /^\|/ { count++ }
      END { print "count:", count }' filename
which looks a bit simpler but is probably a bit less efficient.

For the sake of a little more nit-picking, the end-of-line back-slashes
in your code aren't needed, and you can specify the input filename on
the command line.  This has the advantage that it gets stuck in the
FILENAME variable, so you can run over several input files:

 awk 'oldfilename != FILENAME {
        if (oldfilename != "") print oldfilename ":", count
        count = 0
        oldfilename = FILENAME
      }
      $1 ~ /^\|/ { count++ }
      END { print oldfilename ":", count }' *.extension
--

Patrick TJ McPhee
East York  Canada



Mon, 02 Apr 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. source counts braces in comment lines

2. multi-line comment

3. multi-line comment

4. On-line updates and comments

5. On-line updates and comments

6. On-line updates and comments

7. On-line updates and comments

8. On-line updates and comments

9. On-line updates and comments

10. On-line updates and comments

11. On-line updates and comments

12. On-line updates and comments

 

 
Powered by phpBB® Forum Software