4

Which command should I use to sum the values of two specific columns? For example, I have the file:

1 4 5 1
2 3 5 2
7 8 6 3

And I want to sum the second and last columns, to have the following result

1 4 5 1 5
2 3 5 2 5
7 8 6 3 11

shoud I use awk and bc? I have found many examples to sum the entire column...

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
ziulfer
  • 1,339
  • 5
  • 18
  • 30

4 Answers4

6

Try:

awk '{print $0, $2 + $NF }' input_file
Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
4

Since you tagged the question bash (awk is the most appropriate tool to use!)

#!/bin/bash

while read -a ARRAY
do
    echo ${ARRAY[@]} $((${ARRAY[1]}+${ARRAY[3]}))
done < input.txt

output:

$ ./sum.sh 
1 4 5 1 5
2 3 5 2 5
7 8 6 3 11
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
1

Here is the command to accomplish what you want to:

awk '{$(NF+1)=$NF+$2}1' <filename
anishsane
  • 20,270
  • 5
  • 40
  • 73
1

For simple calculations awk is the way to go. In more complicated situations you may want to parallelize the operation, you can do this with GNU parallel and a calculator of your choice.

With bash:

<infile parallel --colsep ' +' echo '{}' '$(( {2} + {4} ))

With bash and bc:

<infile parallel --colsep ' +' echo '{}' '$(bc <<< "{2} + {4}")'

Note, the current release of parallel doesn't have an easy way to refer to the last element of the input, however, a patch is in the development branch now that allows negative indexing of elements, i.e. you would then be able to use {-1} instead of {4}.

Thor
  • 45,082
  • 11
  • 119
  • 130