LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-31-2006, 08:03 PM   #1
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Rep: Reputation: 30
Recursively count lines of code in source dir heirarchy


I've done a bit of reading on this, but my shell scripting leaves to be desired. I want to be able to output the total number of lines in my entire project, which contains sub-folders. I got this far:
Code:
cat `ls -R1 | grep -v ./ | grep -v .gif | grep -v .jpg`
Passing this to `wc -l', however, doesn't work. In fact, this example is broken at `cat' because I can't figure out how to prepend the full path to the file names (or use any other hack) and I get results like this:
Code:
cat: fck_contextmenu.css: No such file or directory
cat: fck_dialog.css: No such file or directory
cat: fck_editor.css: No such file or directory
simply because those files reside in sub-directories of the cwd but are `ls'ed as ./<file> for all `cat' knows.

Any ideas around this? I'm sure it's quite simple but I can't seem to catch it.
 
Old 03-31-2006, 08:53 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
I usually do something like
Code:
find . \! -iname \*.gif \! -iname \*.jpg -type f -print | xargs wc -l
There are of course several other ways to skin the same cat.
 
Old 03-31-2006, 08:56 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
oh yeah, if your project is very large, you want something like
Code:
find . \! ... -print | xargs cat | wc -l
Reason being that, if xargs sees what would be "too long" a command line for your system, it invokes the command you give it several times
 
Old 03-31-2006, 09:02 PM   #4
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Wrote a short script for you. It uses file instead of grepping, to check if it's a text or ascii file (source files will show up as like, ASCII C++ file text)

Code:
#!/bin/bash

wc_total=0

function cnt {
    for file in `ls $1`; do
	if [ -d $file ]; then
	    cnt $file
	else
	    if [ "`file -b $file | grep -i '\(ascii\|text\)'`" != "" ]; then
		let "wc_total += `cat $file | wc -l`"
	    fi
	fi
    done
	
}

cnt .
echo $wc_total
exit 0

Last edited by 95se; 03-31-2006 at 09:16 PM.
 
Old 03-31-2006, 09:08 PM   #5
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Original Poster
Rep: Reputation: 30
Thank you both! This is a little tidbit I've wanted to add to my reports (just as an FYI) for quite some time. Very much appreciated.
 
Old 04-01-2006, 10:35 AM   #6
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
I just looked at and realized it may be missing one thing, use this one instead...
Code:
#!/bin/bash

wc_total=0

function cnt {
    cd $1
    for file in `ls`; do
	if [ -d $file ]; then
	    cnt $file
	else
	    if [ "`file $file | grep -i '\(ascii\|text\)'`" != "" ]; then
		let "wc_total += `cat $file | wc -l`"
	    fi
	fi
    done
    cd ..
}

cnt .
echo $wc_total
exit 0
 
Old 04-01-2006, 01:56 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I don't think it needs to be that complex; maybe this:
Code:
#!/bin/bash

#'count-lines' script

#put each pattern on it's own line
patterns="`echo "$*" | sed "s/\[ \]+/\n/g"`"

#add the '-name' option to each line
patterns="`echo "$patterns" | sed "s/^/-name /g"`"

#add the '-o' option to all but the last line
patterns="`echo "$patterns" | sed "s/$/ -o/g"`"

cat `find -type f $patterns` | grep -c ".*"
Call the script providing patterns on the command line:
Code:
> count-lines "*.cpp *.hpp *.h"
Sorry, didn't get a chance to try it, but I've used the last line of the script providing patterns "manually". The 'sed' stuff should turn the above patterns into (excluding newlines):
Code:
-name *.cpp -o -name *.hpp -o -name *.h
ta0kira

Last edited by ta0kira; 04-02-2006 at 01:19 PM.
 
Old 04-01-2006, 04:51 PM   #8
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Have you tried sloccount? (http://www.dwheeler.com/sloccount/)
 
Old 04-01-2006, 04:59 PM   #9
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Original Poster
Rep: Reputation: 30
Thank you for the updates and additional scripts, and I will check out sloccount.

I love this forum.
 
  


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count the number of files recursively with wild card redir Linux - Newbie 5 02-28-2006 11:25 AM
rm dir recursively allelopath Linux - General 6 01-06-2006 11:37 AM
how to do yes to all when deleting a dir recursively dr_zayus69 Linux - Newbie 6 12-02-2004 02:42 AM
How to count how many lines of code my project is? The_Nerd Programming 4 08-30-2004 08:26 PM
How to count line numbers recursively in a directory? puzz_1 Linux - General 1 07-01-2004 09:43 AM


All times are GMT -5. The time now is 01:17 PM.

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