Abdel
Abdel

Reputation: 6116

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. 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:

#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...

Any ideas? Please let me know if you need more info!

Upvotes: 0

Views: 218

Answers (3)

glenn jackman
glenn jackman

Reputation: 247240

If you have 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

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77185

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

Upvotes: 1

Kent
Kent

Reputation: 195289

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

Upvotes: 2

Related Questions