Riccardo M. Riccardo M. - 3 months ago 12
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

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
    n=$2
    # No further actions on this line
    next
}
{
    # Add the value of the first field to t(otals)[n(name)]
    t[n]+=$1
}
# Once the end of input has been reached
END{
    # Iterate trough t(otals) keys
    for(n in t) {
        # Print the total along with the name
        print n,t[n]
    }
}