1

I have a table:

  A B C
X 1 2 3 
Y 4 5 6
Z 7 8 9

I want to create two new columns D and E, calculating the average and the value of a formula (A+B)/C respectively to get:

  A B C D E
X 1 2 3 2 1
Y 4 5 6 5 1.5
Z 7 8 9 8 1.67

How to do that? All post I found from search are calculating values in a column but not row, and output to another file.

  • Are the ABCXYZ labels actually part of your file? If so, you'd be better off removing them and just having the data itself in the file. – Wildcard Jan 23 '18 at 3:50
2

Given data.txt containing:

1 2 3
4 5 6
7 8 9

Run:

awk '{$4 = ($1+$2+$3)/3; $5 = ($1+$2)/$3; print}' data.txt

Output will be:

1 2 3 2 1
4 5 6 5 1.5
7 8 9 8 1.66667

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.