9

我想过滤掉第三列中具有“同义词”的行。命令如下

awk '$3 !~ /^synonymous/' fileCSV.csv > fileCSV2.csv

但 fileCSV2.csv 在第 3 列中仍然包含“同义词”一词。我想知道什么可能是错的?

fileCSV.csv 中的两行:

"exonic","LINC00115","synonymous SNV","uc010nxx.2:c.C299T:p.P100L",,"0.99",,0.56,rs3115849,,,,,,,,,,,,,chr1,762273,762273,G,A,"chr1","762273",".","G","A","30483.62","PASS","AC=24;AF=1.00;AN=24;DP=2972;FS=0.000;MLEAC=8;MLEAF=1.00;MQ0=0;VQSLOD=19.50;culprit=FS;set=Intersection","GT:AD:DP:GQ:PL","1/1:0,2:2:6:66,6,0","1/1:0,297:297:99:10476,951,0","1/1:0,304:304:99:10098,950,0","1/1:0,295:295:99:9869,929,0","1/1:0,292:292:99:8655,895,0","1/1:0,304:304:99:10006,965,0","1/1:0,179:179:99:5862,568,0","1/1:0,273:273:99:9328,851,0","1/1:0,279:279:99:7946,850,0","1/1:0,283:283:99:9214,866,0","1/1:0,8:8:21:229,21,0","1/1:0,456:456:99:16385,1285,0"    
"exonic","SAMD11","synonymous SNV","uc001abw.1:c.T1027C:p.W343R","559;Name=lod=249",,,1.00,rs6672356,1,0.916445,N,0.0,T,0.0,B,0.998605,N,4.19E-4,N,3.17,chr1,877831,877831,T,C,"chr1","877831",".","T","C","3594.56","PASS","AC=24;AF=1.00;AN=24;DP=387;FS=0.000;MLEAC=8;MLEAF=1.00;MQ=60.00;MQ0=0;VQSLOD=15.00;culprit=DP;set=Intersection","GT:AD:DP:GQ:PL","1/1:0,3:3:9:97,9,0","1/1:0,3:3:12:113,12,0","1/1:0,64:64:99:1805,189,0","1/1:0,57:57:99:1605,168,0","1/1:0,30:30:90:768,90,0","1/1:0,69:69:99:2026,216,0","1/1:0,15:15:45:428,45,0","1/1:0,23:23:81:809,81,0","1/1:0,22:22:69:562,69,0","1/1:0,40:40:99:1142,117,0","1/1:0,3:3:9:94,9,0","1/1:0,58:58:99:14,7,0"
4

2 回答 2

10

如果您的 fileCSV.csv 的列分隔,比您需要

awk -F, '$3 !~ /^synonymous/' fileCSV.csv > fileCSV2.csv

如果-F不适用于您的awktry版本

awk 'BEGIN{FS=","} $3 !~ /^synonymous/' fileCSV.csv > fileCSV2.csv

编辑:你还需要"考虑,所以使用/^"synonymous/

于 2013-10-31T16:22:41.210 回答
2

要使用 awk 处理 csv 文件,我更喜欢以下方法来自动考虑引号,即使用 sed 进行预处理。

对于您的具体问题,我会使用

sed -e 's/^"//;s/"$//' fileCSV.csv | awk -F '"?,"?' '$3 !~ /^synonymous/'

如果您还想正确处理字符串字段包含引号的文件(在csv文件中将由双引号表示),则需要通过以下方式更改sed表达式,

sed -e 's/^"//;s/"$//;s/""/"/g' fileCSV.csv | awk -F '"?,"?' '$3 !~ /^synonymous/'

此方法的优点是它允许您使用 awk 正确打印或处理某些字段。例如,如果您想打印过滤行中的第一个和第五个字段,用 a 分隔,:您现在可以使用

sed -e 's/^"//;s/"$//;s/""/"/g' fileCSV.csv | awk -F '"?,"?' '$3 !~ /^synonymous/ { print $1,":",$5}'

(如果您不清楚这些方法之间的区别,您可以尝试最后一个不经过 sed 预处理的 awk 命令)

于 2014-06-04T09:03:28.973 回答