0

I have file like this. Its called a sam file

@HD VN:1.0  SO:unsorted
@SQ SN:RNU2-1   LN:388
@SQ SN:RNU6-1100P   LN:304
SRR959756.23    0   RNU2-1  56  0   6S32M12S    *   0   0   CACAGCAATATCTGATACGTCCTCTATCCGAGGACAATTGGAATTCTCGG  hhhhhhhhhhhhhhhhhhhhhhghhghhhhhhhhhhhhhhfhhhhhhghf  AS:i:64 XS:i:64 XN:i:0  XM:i:0  XO:i:0  XG:i:0  NM:i:0  MD:Z:32 YT:Z:UU
SRR959756.23    256 RNU2-2P 56  0   6S32M12S    *   0   0   CACAGCAATATCTGATACGTCCTCTATCCGAGGACAATTGGAATTCTCGG  *   AS:i:64 XS:i:64 XN:i:0  XM:i:0  XO:i:0  XG:i:0  NM:i:0  MD:Z:32 YT:Z:UU
SRR959756.23    256 RNU2-6P 55  0   6S32M12S    *   0   0   CACAGCAATATCTGATACGTCCTCTATCCGAGGACAATTGGAATTCTCGG  *   AS:i:56 XS:i:64 XN:i:0  XM:i:4  XO:i:0  XG:i:0  NM:i:1  MD:Z:11T20  YT:Z:UU

I want to filter the file on 15th column which is XM so i wrote this

awk '$15 ~ /^XM:i:0/ || $15 ~ /^XM:i:1/ || $15 ~ /^XM:i:2/ || $15 ~ /^XM:i:3/ {print $0}' file.txt

In doing so, I lost the headers which begins with @(the first three lines). How can I keep them as it is in the output.

The output should be like this

@HD VN:1.0  SO:unsorted
@SQ SN:RNU2-1   LN:388
@SQ SN:RNU6-1100P   LN:304
SRR959756.23    0   RNU2-1  56  0   6S32M12S    *   0   0   CACAGCAATATCTGATACGTCCTCTATCCGAGGACAATTGGAATTCTCGG  hhhhhhhhhhhhhhhhhhhhhhghhghhhhhhhhhhhhhhfhhhhhhghf  AS:i:64 XS:i:64 XN:i:0  XM:i:0  XO:i:0  XG:i:0  NM:i:0  MD:Z:32 YT:Z:UU
SRR959756.23    256 RNU2-2P 56  0   6S32M12S    *   0   0   CACAGCAATATCTGATACGTCCTCTATCCGAGGACAATTGGAATTCTCGG  *   AS:i:64 XS:i:64 XN:i:0  XM:i:0  XO:i:0  XG:i:0  NM:i:0  MD:Z:32 YT:Z:UU

Also header lines(starting with @) can be many more like in hundreds. I just used first 3 lines.

1 Answer 1

0

I tried this and it worked

awk '$0 ~ /^@/ || $15 ~ /^XM:i:0/ || $15 ~ /^XM:i:1/ || $15 ~ /^XM:i:2/ || $15 ~ /^XM:i:3/ {print $0}' file.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .