LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-11-2003, 12:29 AM   #1
itsjvivek
LQ Newbie
 
Registered: Dec 2002
Posts: 16

Rep: Reputation: 0
Exclamation counting the commented lines using awk [ /* */]


Hello Everybody

I am writing a shell script to count the lines of code in a c program

I am able to find out the single comment lines

i am facing problem when i try to find out the multiple comment lines

EX:

/* This is a
multi line comment
by the author */

could anyout there give me the awk script for this particular
matching

Thankz in advance
 
Old 01-11-2003, 01:18 AM   #2
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
What have you got so far?
 
Old 01-11-2003, 04:08 AM   #3
itsjvivek
LQ Newbie
 
Registered: Dec 2002
Posts: 16

Original Poster
Rep: Reputation: 0
I am able to find the comment lines

1. starting with [ // ]
2. single line comments [ /* asdadfadf */ ]

I am clueless about find the multi line comments

/* adfasdf
asdfasdf
asdfasdf
sdfasdf */
 
Old 01-11-2003, 11:38 AM   #4
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
Post your code. . .
 
Old 01-13-2003, 07:43 AM   #5
itsjvivek
LQ Newbie
 
Registered: Dec 2002
Posts: 16

Original Poster
Rep: Reputation: 0
#!/bin/bash

##################################
# Getting The User Input
##################################

echo "Enter The Source Directory Path"
echo "Ex : /carina/tools/src"
read SRC_PATH
#SRC_PATH="/home/orbjvi/cnt"
#SRC_PATH="/home/orbjvi/as400-test/secrvr"


##################################
# Validating The User Input
##################################

if test -z $SRC_PATH
then
echo "Path Not Entered!!!"
echo "Quitting..."
exit
else
if test -d $SRC_PATH
then
#echo "Do You Want To Scan Recursively?[Y/N]"
#read RECURSION
#if [ $RECURSION = y -o $RECURSION = Y -o $RECURSION = yes -o $RECURSION = YES -o $RECURSION = Yes ]
# then
# SEARCH="find $SRC_PATH
echo "Starting...."
echo
#echo
#echo
#fi
else
echo "Path Not Valid!!!"
echo "Quitting..."
exit
fi
fi

##################################
# Checking For Blank Lines
##################################

for Filename in $(find $SRC_PATH -iname "*.c")
do
echo Checking `basename $Filename`

awk 'BEGIN { }

NF > 0 { ++TOTAL }
/^$/ { ++BLANK }
/\/\// { ++SLASH }
/\/\*/ { ++MULTI }

# Similarly i want to find
# multiline comments

END {
printf("TOTAL = %4d\n" , TOTAL);
printf("BLANKS = %4d\n" , BLANK);
printf("SINGLES = %4d\n" , SLASH);
printf("COMMENT1 = %4d\n" , MULTI);
printf("\n");
} ' $Filename

done
 
Old 01-15-2003, 03:40 AM   #6
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
There are programs available, but. . .

I would imagine that a way to do this (I haven't done it, but I've
got ideas) would be to keep an eye out for the begin multi-line
comment (/*), and hop in to a while loop, during which you look for
new lines (\n) and increment. The while loop will terminate
when you come to an end multi-line comment (*/).
 
Old 01-15-2003, 07:45 AM   #7
itsjvivek
LQ Newbie
 
Registered: Dec 2002
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks moses

even i was having the same idea

but since i am just one week new to shell script , i am finding
it difficult to implement in code

if you could get it in code .. just give me

or please give me links where any sample code coulde be available

Thanks in advance
 
Old 01-17-2003, 09:03 AM   #8
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
Ok, Here's my little awk script (on its own, you'll have to modify it
somewhat to incorporate it into yours):

Code:
awk -F"\n"  
'BEGIN
{RS=/\/\*/}
{RT=/\*\//} 
{++MULTI} 
END 
{print MULTI}'
commented code:

Code:
awk
'BEGIN
{FS="\n"}    # set the field separator to be a newline
{RS=/\/\*/}   # set the Record separator to be the begin multiline comment 
{RT=/\*\//}   # set the Record terminator to be the end multiline comment
{++MULTI}
END 
{print MULTI}'
Try this out, for more help read the awk man page. . .
[edit]:
Actually, there is a bug in here, but I haven't found it (them?) yet.

Last edited by moses; 01-17-2003 at 09:19 AM.
 
Old 01-17-2003, 09:30 AM   #9
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
Here is a fixed version. I'm leaving the other one up there so
someone who knows more can look at it and tell me what's wrong
with it, if they care to. . .

Code:
awk '/\/\*/,/\*\//   # Set the range pattern to be that of a multiline comment
{++MULTI}  # Increment the multiline counter
END 
{print MULTI}'
Again, the man page is your friend. . .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 03:03 PM
Counting Lines ej25 Programming 20 12-06-2004 03:08 PM
awk text that is on several lines homey Programming 2 10-31-2004 10:27 AM
/etc/securetty --> I commented out all lines and I can still log in as root adamrau Linux - Security 2 05-30-2004 07:16 AM
how to remove all commented lines win32sux Linux - General 11 05-08-2004 04:27 AM


All times are GMT -5. The time now is 09:14 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration