linux - Sum numbers in lines grouped in blocks with headers (with Bash and AWK) -
assuming have following example output:
node hostabc foofoofoo 1 foo 1 ... 1 ... 1 ... 1 ... 5 ... node hostcde ... 1 ... 10 ...
how can sum of numbers each host (without using temporary files, , using bash , awk only)?
for example:
hostabc 10 hostcde 11
you can use following awk
command:
awk '/^node/{n=$2;next}{t[n]+=$1}end{for(n in t){print n,t[n]}}' file
better explained multiline script:
# if line starts 'node' /^node/ { # set n(ame) value of second field n=$2 # no further actions on line next } { # add value of first field t(otals)[n(name)] t[n]+=$1 } # once end of input has been reached end{ # iterate trough t(otals) keys for(n in t) { # print total along name print n,t[n] } }
Comments
Post a Comment