user3659451
user3659451

Reputation: 2073

awk issue, summing lines in various files

I have a list of files starting with the word "output", and I want to sum up the total number of rows in all the files.

Here's my strategy:

for f in `find outpu*`;do wc -l $f | awk '{x+=$1}END{print $1}' ; done

Before piping over, if there were a way I could do something like >> to a temporary variable and then run the awk command after, I could accomplish this goal.

Any tips?

Upvotes: 0

Views: 66

Answers (3)

rook
rook

Reputation: 6240

Here is some stuff for fun, check it out:

grep -c . out* | cut -d':' -f2- | paste -sd+ | bc

all lines, including empty ones:

grep -c '' out* | cut -d':' -f2- | paste -sd+ | bc

you can play in grep with conditions on lines in files

Upvotes: 1

Farvardin
Farvardin

Reputation: 5414

use this to see details and sum :

wc -l output*

and this to see only the sum:

wc -l output* | tail -n1 | cut -d' ' -f1

Upvotes: 2

joepd
joepd

Reputation: 4841

Watch out, this find command will only find stuff in your current directory if there is one file matching outpu*.

One way of doing it:

awk 'END{print NR}' $(find 'outpu*')

Provided that there is not an insane amount of matching filenames that overflows the maximum command length limit of your shell.

Upvotes: 0

Related Questions