Riccardo M. Riccardo M. - 1 year ago 73
Bash Question

Sum numbers in lines grouped in blocks with headers (with Bash and AWK)

Assuming I have the following example output:

Node hostabc foofoofoo
1 foo
1 ...
1 ...
1 ...
1 ...
5 ...
Node hostcde ...
1 ...
10 ...

How can I get the sum of the numbers for each host (without using temporary files, and using Bash and AWK only)?
For example:

hostabc 10
hostcde 11

Thanks in advance for any help! :)

Answer Source

You can use the following awk command:

awk '/^Node/{n=$2;next}{t[n]+=$1}END{for(n in t){print n,t[n]}}' file

Better explained as a multiline script:

# If the line starts with 'Node'
/^Node/ {
    # Set n(ame) to the value of the second field
    # No further actions on this line
    # Add the value of the first field to t(otals)[n(name)]
# Once the end of input has been reached
    # Iterate trough t(otals) keys
    for(n in t) {
        # Print the total along with the name
        print n,t[n]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download