Go Back   Technology Questions > Software Questions > Operating System Questions > Linux

Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old 06-07-2008, 07:40 AM
Aaron Gray
Tablet PC Guest
 
Posts: n/a
Getting loc

Hi,

I have asked this question before but cannot find the thread on Google
Groups.

Basicaly I want a line of shell code that counts the number of non blank
lines of code in a file, directory or directory tree.

Many thanks in advance,

Aaron


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

 
Old 06-07-2008, 07:40 AM
Xploder HD Movie Player for PS3. Manage, convert and transfer media files between the PC and PS3.
  #2 (permalink)  
Old 06-07-2008, 07:50 AM
Lew Pitcher
Tablet PC Guest
 
Posts: n/a
Re: Getting loc

In alt.os.linux, Aaron Gray wrote:

> Hi,
>
> I have asked this question before but cannot find the thread on Google
> Groups.
>
> Basicaly I want a line of shell code that counts the number of non blank
> lines of code in a file, directory or directory tree.


First off, define what you mean by "non blank lines of code".
Some questions to consider:
/What/ code? (shell script code? java? c++? c? perl? awk? TCL? each
language has it's own concept of what a "line of code" consists of)

Do compound statements count as one line or multiple lines? (Consider
what the C " { something(); something_else(); result=third_thing(); } "
means; is this one, three, four, or five lines of code?

How do you distinguish a "line of code" from other, extraneous lines?
(How do you handle comment lines, for instance?)

How do you want to accomodate "included" files (i.e. source files that
one /or more/ other sources "#include")? Do you count lines once in each
of these files, or do you count them for each time the source file is
included into another source file?

Once you've answered these questions, post your responses, and we'll have a
better understanding of what it is you want to count. From that, we'll be
able to properly provide suggestions on how to count your "lines of code".

> Many thanks in advance,
>
> Aaron


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3 (permalink)  
Old 06-07-2008, 08:00 AM
Lew Pitcher
Tablet PC Guest
 
Posts: n/a
Re: Getting loc

In alt.os.linux, Benny Nielsen wrote:

> Aaron Gray wrote:
>
>> Hi,
>>
>> I have asked this question before but cannot find the thread on Google
>> Groups.
>>
>> Basicaly I want a line of shell code that counts the number of non blank
>> lines of code in a file, directory or directory tree.
>>
>> Many thanks in advance,
>>
>> Aaron

>
> cat file(s) | grep -v '^$' | wc -l


1) Useless use of cat
Try grep -v '^$' file | wc -l
or grep -v -R '^$' directory | wc -l

2) doesn't properly solve the problem
a) miscounts comments as lines of code
b) doesn't properly count lines of code
~/tmp $ cat file.c
/*
** this
** is
** a
** very
** long
** comment
*/
int main(void) { return 0;}
~/tmp $ cat file.c | grep -v '^$' | wc -l
9
Result should be either 1 or 2, depending on
OP's definition of "line of code"

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #4 (permalink)  
Old 06-07-2008, 08:00 AM
Benny Nielsen
Tablet PC Guest
 
Posts: n/a
Re: Getting loc

Aaron Gray wrote:

> Hi,
>
> I have asked this question before but cannot find the thread on Google
> Groups.
>
> Basicaly I want a line of shell code that counts the number of non blank
> lines of code in a file, directory or directory tree.
>
> Many thanks in advance,
>
> Aaron


cat file(s) | grep -v '^$' | wc -l
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #5 (permalink)  
Old 06-07-2008, 08:10 AM
Aaron Gray
Tablet PC Guest
 
Posts: n/a
Re: Getting loc

"Lew Pitcher" <lpitcher@teksavvy.com> wrote in message
news:498ce$484aacdc$4c0a8c73$12692@TEKSAVVY.COM-Free...
> In alt.os.linux, Aaron Gray wrote:
>
>> Hi,
>>
>> I have asked this question before but cannot find the thread on Google
>> Groups.
>>
>> Basicaly I want a line of shell code that counts the number of non blank
>> lines of code in a file, directory or directory tree.

>
> First off, define what you mean by "non blank lines of code".
> Some questions to consider:
> /What/ code? (shell script code? java? c++? c? perl? awk? TCL? each
> language has it's own concept of what a "line of code" consists of)


C++

> Do compound statements count as one line or multiple lines? (Consider
> what the C " { something(); something_else(); result=third_thing(); } "
> means; is this one, three, four, or five lines of code?


1

> How do you distinguish a "line of code" from other, extraneous lines?
> (How do you handle comment lines, for instance?)


Comments can be counted too.

> How do you want to accomodate "included" files (i.e. source files that
> one /or more/ other sources "#include")? Do you count lines once in each
> of these files, or do you count them for each time the source file is
> included into another source file?


Only once.

> Once you've answered these questions, post your responses, and we'll have
> a
> better understanding of what it is you want to count. From that, we'll be
> able to properly provide suggestions on how to count your "lines of code".


I have used "grep -v -R '^$' directory | wc -l" which gives about 640000
lines of code in the project.

I was just after a ready reconer.

Thanks,

Aaron


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #6 (permalink)  
Old 06-18-2008, 05:10 PM
Jim Diamond
Tablet PC Guest
 
Posts: n/a
Re: Getting loc

On 2008-06-07, Aaron Gray <ang.usenet******.com> wrote:
> "Lew Pitcher" <lpitcher@teksavvy.com> wrote in message
> news:498ce$484aacdc$4c0a8c73$12692@TEKSAVVY.COM-Free...
>> In alt.os.linux, Aaron Gray wrote:
>>
>>> Hi,
>>>
>>> I have asked this question before but cannot find the thread on Google
>>> Groups.
>>>
>>> Basicaly I want a line of shell code that counts the number of non blank
>>> lines of code in a file, directory or directory tree.


> I have used "grep -v -R '^$' directory | wc -l" which gives about 640000
> lines of code in the project.


Bug: That counts a line with (only) whitespace as a line of code.
Try '^[ ]*$' instead. Note that is a space and a tab inside
the brackets.

Cheers.

Jim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 02:00 PM.


2003 - 2008 All Rights Reserved. Technology Questions

SEO by vBSEO 3.1.0