首页 > 解决方案 > How to subtract each value in a column from same number in a .txt file in Linux?

问题描述

I have a a .txt file (mydata.txt) in Linux and want to subtract each value in one column and save the output values in a new column alongside with the old column. For example, in the data example below, I subtract each value in C1 column from 2 and saved the output column (C1_sub) as a new column, output of C2 in column C2_sub and so on. How do I do it if I have one or multiple columns?

C1  C1_sub  C2  C2_sub  C3  C3_sub
1   1   2   0   2   0
0   2   1   1   2   0
2   0   0   2   0   2
0   2   2   0   1   1
0.008   1.992   0   2   2   0
1.999   0.001   1   1   0   2
0   2   2   0   1   1
0   2   0.001   1.999   2   0
1   1   1   1   0   2
2   0   2   0   0.013   1.987
0   2   0.999   1.001   0   2
0   2   2   0   1   1
1   1   1.999   0.001   2   0
1.99    0.01    1.999   0.001   2   0
0   2   1.999   0.001   1   1

标签: linuxsubtraction

解决方案


awk 'NR==1{print "C1", "C1_sub", "C2", "C2_sub", "C3", "C3_sub"; next} 
    { print $1, 2 - $1,$2, 2 - $2, $3, 2-$3}' OFS=\\t input

推荐阅读