Search This Blog

awk: append a column that sums up existing columns

E.g. we have a text file, numbers.txt, see below:
1 2
3 4
5 6

and we want to add the 3rd column which is the sum of values of the first and the second columns.
1 2 3
3 4 7
5 6 11

The following command:
awk '{print $1 " " $2 " " int($1+$2)}' numbers.txt > new_numbers.txt
will do the work.
Or, the following command do the same thing:
awk '{printf "%s %s %d\n",$1,$2,int($1+$2)}' numbers.txt > new_numbers.txt



No comments:

Post a Comment