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

Re: OT: line count in a Dir(Solved - My way-Kindda)



Hi,

That is what i was looking for...but I did it myself...here is what was
needed:

I needed to know the total number of lines in the whole CVS tree.The
tree is almost all Unix based, but it also had a cvs checking from a
developer who had compiled his sources in Win32, and hence had some
files with spaces..

So, I used:

[17:03:18][kevin@tokyo:~]$ find . -type f -print | sed 's/ /\\\\' |
xargs \
> wc -l | grep ' total $' | awk '{sum = sum + $1| ; END {print sum}' 

Which took care of all..

But, I must say, Steve's shell script is really nice.

Thanks to all you guyz...

PS - I got another suggestion from dave, who remarked about -print0.This
should work in other versions of find, but unfortunetly Solaris's find
does not have this option..(or I could'nt find it anyway..)...

Regards

-Kevin

Steve Williams wrote:
> 
> 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