Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

sed

awk

bc

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

like image 207
ziulfer Avatar asked Dec 16 '22 14:12

ziulfer


1 Answers

Try:

awk '{print $0, $2 + $NF }' input_file
like image 75
Hui Zheng Avatar answered May 11 '23 17:05

Hui Zheng