tags:

views:

839

answers:

6

I was trying to use sed to count all the lines based on a particular extension.

find -name '*.m' -exec wc -l {} \; | sed ...

I was trying to do the following, how would I include sed in this particular line to get the totals.

+2  A: 

you could use sed also for counting lines in place of wc:

 find . -name '*.m' -exec sed -n '$=' {} \;

where '$=' is a "special variable" that keep the count of lines

EDIT

you could also try something like sloccount

dfa
find . -name '*.m' -exec sed -n 'where $=' {} \;Is this it?
Berlin Brown
That is not the total though, added together.
Berlin Brown
OK, I ended up with this.$ find . -name '*.m' -exec sed -n '$=' {} \; | sum -22696 1
Berlin Brown
+3  A: 

You can cat all files through a single wc instance to get the total number of lines:

find . -name '*.m' -exec cat {} \; | wc -l
sth
+4  A: 

You may also get the nice formatting from wc with :

wc `find -name '*.m'`
Manu
That is a nice approach.
Berlin Brown
Add `-type f` to avoid the case of a directory name that matches: `wc $(find -type f -name '*.m')`
Dennis Williamson
+2  A: 

Hm, solution with cat may be problematic if you have many files, especially big ones.

Second solution doesn't give total, just lines per file, as I tested.

I'll prefer something like this:

find . -name '*.m' | xargs wc -l | tail -1

This will do the job fast, no matter how many and how big files you have.

igustin
+1  A: 

sed is not the proper tool for counting. Use awk instead:

find . -name '*.m' -exec awk '{print NR}' {} +

Using + instead of \; forces find to call awk every N files found (like with xargs).

marco
Funny, I actually meant to say awk
Berlin Brown
A: 

For big directories we should use:

find . -type f -name '*.m' -exec sed -n '$=' '{}' + 2>/dev/null | awk '{ total+=$1 }END{print total}' 

# alternative using awk twice
find . -type f -name '*.m' -exec awk 'END {print NR}' '{}' + 2>/dev/null | awk '{ total+=$1 }END{print total}'