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

StackOverflow https://stackoverflow.com/questions/14832515

  •  09-03-2022
  •  | 
  •  

Вопрос

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

Это было полезно?

Решение

Try:

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

Другие советы

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

Here is the command to accomplish what you want to:

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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top