[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OT: line count in a Dir



Hi,

It will give multiple totals, one for each "wc -l" that xargs forks off
In a small tree, there will only be one wc -l forked.  For large
trees, it will take multiple wc -l's...

xargs also pukes if there are spaces in filenames... be warned...

Something like...

( grindingly slow, but guaranteed work on any filename and very portable )...

#!/bin/sh
total=0
find . -type f -print | while read file; do
  lines=`wc -l "$file" | awk '{ print $1 }'`
  total=`expr $total + $num`
done
echo "Total Lines: $total"

**** NOTE **** 
This is untested, is painfully slow, as it is written for
maximum accuracy ( note ""'s around $file in the wc - important for
filenames with spaces in them  AND maximum portability.

I am also aware using ksh's ( bash??), there are builtin's for
doing the addiation.  The optomization of the above for a particular
shell is left as an exercise for the reader


If "close enough" is fine, then I'd do...

find . -type f -print | xargs wc -l 2>/dev/null | \
	awk '$2 == "total" { sum += $1 } END { print "Total: " sum }'

( after making sure the "total" line from wc -l is actually the exact
word total, and not "Total", "total:" etc...

and a combination of the two would be more efficient ( 2 less processes/file)
and almost as portable..

#!/bin/sh
total=0
find . -type f -print | while read file; do
  wc -l "$file" 
done | awk '{ sum += $1 } END { print "Total: " sum }'

You should be able to get something out of all of these..

the main difference is whether you have filenames with spaces in them

Cheers!
> 
> 
> >I should have been a bit more descriptive. We have a huge CVS
> >development tree, and I was told to find the number of lines of code we
> >have in the whole tree...(i.e. Sum). Any thoughts on how this can be
> >done?
> 
> Alexander Farber's suggestion:
> 
>   find . -type f -print | xargs wc -l
> 
> should give a total at the end.  You can pipe that through tail
> if you just want the total, e.g.:
> 
> jill@cauldron:bsddb3$ find . -name \*.py | xargs wc -l | tail -1
>     3121 total
> 
> Jill Lundquist		                           jill@chezns.org
> "The first butcher I saw as a child had a wooden leg, and to this
>  day I have an unreasonable feeling that butchers with two genuine
>  legs are impostors."            -Robertson Davies
> 


-- 
	Steve Williams, Calgary, Alberta, Canada
	Genie Computer Systems Inc.
	steve@genie96.com

"A man doesn't begin to attain wisdom until he recognizes that he is 
 no longer indispensable."
- Admiral Richard E. Byrd ( 1888-1957 )