0

我有一个包含许多列的文件,我想在第 11 列和第 9 列之间执行划分并在新行中打印。

ram, gopal, shayam, raj, kamal, shekhar, uday, bhanu, 10,uat,20  
ram, gopal, shayam, raj, kamal, shekhar, uday, bhanu, 20,uat,60  
ram, gopal, shayam, raj, kamal, shekhar, uday, bhanu, 30,uat,90  

输出应如下所示 -

ram, gopal, shayam, raj, kamal, shekhar, uday, bhanu, 10,uat,20,2%  
ram, gopal, shayam, raj, kamal, shekhar, uday, bhanu, 20,uat,60,3%  
ram, gopal, shayam, raj, kamal, shekhar, uday, bhanu, 30,uat,90,3%  

致命除以 0 错误:

$ awk -F, -vOFS=, '{print $0,($9==0?0:$11/$9)"%"}' /cygdrive/c/test/kpi/test.csv
awk: cmd. line:1: (FILENAME=/cygdrive/c/test/kpi/test.csv FNR=1) fatal: division by zero attempted
4

1 回答 1

3

可以这样做:

$ awk -F, -vOFS=, '{print $0,$11/$9"%"}' file
ram, gopal, shayam, raj, kamal, shekhar, uday, bhanu, 10,uat,20,2%
ram, gopal, shayam, raj, kamal, shekhar, uday, bhanu, 20,uat,60,3%
ram, gopal, shayam, raj, kamal, shekhar, uday, bhanu, 30,uat,90,3%
于 2013-10-25T08:43:28.360 回答