From: Dion on
I am a beginner in UNIX and I want to write a shell script calculating
the proportion of one-line comments in any C++ program. Could anyone
help me please? Thanks!

From: Loki Harfagr on
Le Sun, 23 Oct 2005 13:27:20 -0700, Dion a ?crit?:

> I am a beginner in UNIX and I want to write a shell script calculating
> the proportion of one-line comments in any C++ program. Could anyone
> help me please? Thanks!

No problem, just give here a short sample of input file
and desired output, crystal ball's on duty at the moment :-)
From: Dion on
It can be even the simple "Hello World" program. Let's say I have
something like that:

// This is my program
// I want to calculate its comments' proportion vs. the whole C++
program
// Let'try

int main()
{
cout << "Hello Wolrd!" << endl;

return 0;
}

>From this program I would like to output that the 3 commented lines are
the 33.33% of the whole program's lines.


Thanks a lot for your help!

From: Loki Harfagr on
Le Sun, 23 Oct 2005 13:43:33 -0700, Dion a ?crit?:

> It can be even the simple "Hello World" program. Let's say I have
> something like that:
>
> // This is my program
> // I want to calculate its comments' proportion vs. the whole C++
> program
> // Let'try
>
> int main()
> {
> cout << "Hello Wolrd!" << endl;
>
> return 0;
> }
>
>>From this program I would like to output that the 3 commented lines are
> the 33.33% of the whole program's lines.

Well, your given input file is either 10 or 11 lines, thus it'll never
give out 33.33p100 ..
Anyway, a quick toolbox solution would be :

$ (fayle=pathtoyoursourcefile;
echo "scale=2;100*"$(grep -c '^//' ${fayle} )"/"$(grep -c '' ${fayle}) )|bc

You could do it in awk as well, but ...-)
From: Chris F.A. Johnson on
On 2005-10-23, Dion wrote:
> It can be even the simple "Hello World" program. Let's say I have
> something like that:
>
> // This is my program
> // I want to calculate its comments' proportion vs. the whole C++
> program
> // Let'try
>
> int main()
> {
> cout << "Hello Wolrd!" << endl;
>
> return 0;
> }
>
>>From this program I would like to output that the 3 commented lines are
> the 33.33% of the whole program's lines.

If you get that answer, your script is wrong.

awk '/^\/\// { ++comments }
END { print comments * 100 / NR "%" }'


--
Chris F.A. Johnson <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>