简体   繁体   English

unix / bash:如何仅打印列中的某些字符串

[英]unix/bash: how to print only certain strings from a column

I have a file with >1 million lines that look like this: 我有一个大于一百万行的文件,如下所示:

#CHROM POS ID REF ALT QUAL FILTER INFO
1 63735 . CCTA C 106563.91 PASS AC=422;AF=0.301;AN=1401;BaseQRankSum=-18.154;DP=5730;FS=22.693;HOMLEN=3;HOMSEQ=CTA;HRun=0;HaplotypeScore=7.6359;InbreedingCoeff=-0.0873;MQ=26.67;MQ0=1215;MQRankSum=6.893;QD=18.67;ReadPosRankSum=7.611;SB=-51269.38;SVTYPE=DEL;VQSLOD=1.6440;culprit=InbreedingCoeff;set=UG-PINDEL
1 83631 . GT G 29190.62 PASS AC=517;AF=0.398;AN=1298;BaseQRankSum=8.994;DP=2724;FS=14.345;HOMLEN=2;HaplotypeScore=2.8768;InbreedingCoeff=-0.0858;MQ=16.73;MQ0=6144;MQRankSum=22.879;QD=5.63;ReadPosRankSum=-4.971;SB=0.00;SVTYPE=DEL;HOMSEQ=TT;HRun=3;VQSLOD=1.2361;culprit=FS;set=UG-PINDEL
1 125797 . CAAAAT C 2015.26 PASS AC=42;AF=0.039;AN=1084;BaseQRankSum=-0.600;DP=1083;FS=4.122;HOMLEN=3;HOMSEQ=AAA;HRun=0;HaplotypeScore=0.6543;InbreedingCoeff=-0.0391;MQ=11.09;MQ0=2508;MQRankSum=0.338;QD=3.86;ReadPosRankSum=-1.262;SB=-81.35;SVTYPE=INS;VQSLOD=3.1685;culprit=QD;set=UG-PINDEL
1 572203 . AC A 2292.53 PASS AC=62;AF=0.041;AN=1520;BaseQRankSum=-0.597;DP=7721;FS=3.807;HOMLEN=3;HOMSEQ=CCC;HRun=4;HaplotypeScore=3.4199;InbreedingCoeff=-0.0516;MQ=21.98;MQ0=7155;MQRankSum=-10.737;QD=1.41;SB=-0.71;SVTYPE=DEL;ReadPosRankSum=0.803;VQSLOD=2.0910;culprit=FS;set=UG-PINDEL

As you can see, the eighth column (the one with "INFO" as a header) consists of lots of info separated by semicolons. 如您所见,第八列(标题为“ INFO”的列)包含许多信息,并用分号分隔。 I want to print this same file, but instead of all that info in the eight column, I want the eighth column to only print "SVTYPE=DEL" or "SVTYPE=INS", so the new file looks like this: 我想打印相同的文件,但我不想在第八列中打印所有信息,而是希望在第八列中仅打印“ SVTYPE = DEL”或“ SVTYPE = INS”,因此新文件如下所示:

#CHROM POS ID REF ALT QUAL FILTER INFO
1 63735 . CCTA C 106563.91 PASS SVTYPE=DEL
1 83631 . GT G 29190.62 PASS SVTYPE=DEL
1 125797 . CAAAAT C 2015.26 PASS SVTYPE=INS
1 572203 . AC A 2292.53 PASS SVTYPE=DEL

A simple awk statement where I treat the eighth column as multiple columns separated by a semicolon does not work, because the "SVTYPE=DEL" or "SVTYPE=INS" does not always come in the same column then... 一个简单的awk语句无法将第八列视为由分号分隔的多个列,因为“ SVTYPE = DEL”或“ SVTYPE = INS”并不总是出现在同一列中...

Any ideas? 有任何想法吗? Please let me know if you need more info! 如果您需要更多信息,请告诉我!

like this? 像这样? (header omitted) (标题省略)

kent$  awk '$8=$8~/=DEL/?"SVTYPE=DEL":"SVTYPE=INS"' file
1 63735 . CCTA C 106563.91 PASS SVTYPE=DEL
1 83631 . GT G 29190.62 PASS SVTYPE=DEL
1 125797 . CAAAAT C 2015.26 PASS SVTYPE=INS
1 572203 . AC A 2292.53 PASS SVTYPE=DEL

If you have GNU awk: 如果您有GNU awk:

awk 'match($8, /SVTYPE=[^;]+/, a) {$8=a[0]} 1'

You don't have to do anything special with the header line. 您不必对标题行做任何特殊的事情。

http://www.gnu.org/software/gawk/manual/html_node/String-Functions.html#index-g_t_0040code_007bmatch_0028_0029_007d-function-1405 http://www.gnu.org/software/gawk/manual/html_node/String-Functions.html#index-g_t_0040code_007bmatch_0028_0029_007d-function-1405

This should do the trick: 这应该可以解决问题:

awk '
BEGIN { 
    print "#CHROM POS ID REF ALT QUAL FILTER INFO" 
} 
NR>1 { 
    for (i=1; i<=NF;i++) if (i<8 || $i~/SVTYPE/) {
        printf $i" "
    }; 
    print "" 
}' FS="[ ;]" temp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM