Creating new columns calculating the value in the same row in Linux
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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.
text-processing awk
add a comment |Â
up vote
1
down vote
favorite
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.
text-processing awk
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 at 3:50
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
text-processing awk
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.
text-processing awk
edited Jan 23 at 7:25
ñÃÂsýù÷
15.2k92462
15.2k92462
asked Jan 23 at 3:35
Johnny Tam
1208
1208
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 at 3:50
add a comment |Â
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 at 3:50
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 at 3:50
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 at 3:50
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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
add a comment |Â
up vote
2
down vote
accepted
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
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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
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
answered Jan 23 at 3:52
Wildcard
22k855154
22k855154
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418996%2fcreating-new-columns-calculating-the-value-in-the-same-row-in-linux%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 at 3:50