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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-10-2003, 11:29 PM
|
#1
|
|
LQ Newbie
Registered: Dec 2002
Posts: 16
Rep:
|
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
|
|
|
|
01-11-2003, 12:18 AM
|
#2
|
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
What have you got so far?
|
|
|
|
01-11-2003, 03:08 AM
|
#3
|
|
LQ Newbie
Registered: Dec 2002
Posts: 16
Original Poster
Rep:
|
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 */
|
|
|
|
01-11-2003, 10:38 AM
|
#4
|
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
Post your code. . .
|
|
|
|
01-13-2003, 06:43 AM
|
#5
|
|
LQ Newbie
Registered: Dec 2002
Posts: 16
Original Poster
Rep:
|
#!/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
|
|
|
|
01-15-2003, 02:40 AM
|
#6
|
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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 (*/).
|
|
|
|
01-15-2003, 06:45 AM
|
#7
|
|
LQ Newbie
Registered: Dec 2002
Posts: 16
Original Poster
Rep:
|
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
|
|
|
|
01-17-2003, 08:03 AM
|
#8
|
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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 08:19 AM.
|
|
|
|
01-17-2003, 08:30 AM
|
#9
|
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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. . .
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:54 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|