ziulfer
ziulfer

Reputation: 1369

bash command to sum and print each element of a column and print a new column

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...

Upvotes: 4

Views: 568

Answers (4)

Thor
Thor

Reputation: 47189

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}.

Upvotes: 1

Fredrik Pihl
Fredrik Pihl

Reputation: 45670

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

Upvotes: 4

Hui Zheng
Hui Zheng

Reputation: 10222

Try:

awk '{print $0, $2 + $NF }' input_file

Upvotes: 6

anishsane
anishsane

Reputation: 20980

Here is the command to accomplish what you want to:

awk '{$(NF+1)=$NF+$2}1' <filename

Upvotes: 1

Related Questions