using nawk, how to sum all numbers in $1, $2, and $3 fields?
8 6 2
7 3 5
1 9 4
I want the result to be sum = 45
|
|
|
|||
|
|
|
using awk:
This will work irrespective of the number of rows or columns. By using the record separator(RS) as space or a newline, every value is split into a separate line, and hence can be easily added. |
|||
|
|
|
You could also go with coreutils and bc:
If some columns should be omitted, e.g. if the input is like this:
Output in both cases:
|
|||
|
|