tyebillion tyebillion - 3 months ago 8
Bash Question

How to combine the results of two or more wc commands?

How to combine the results of two or more wc commands?

wc *.foo
wc *.bar


To view the sum total for the lines, word count and bytes?

Here is what I have so far:

wc *.foo|grep total
wc *.bar|grep total


But when I try to combine the commands I get strange behaviour on the command prompt:

{ wc *.foo & wc *.bar; }|grep total


I would like to pass the output to awk to calculate the sums:

awk '{l+=$1; w+=$2; b+=$3} END {print "total lines: " l ", words: " w ", bytes: " b}'

Answer
wc *.foo *.bar | awk '/total/ {print "total lines: " $1 ,"words: " $2, "bytes: " $3}'
total lines: 18 words: 23 bytes: 53
Comments