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.

1
  • 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. Commented Jan 23, 2018 at 3:50

1 Answer 1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.