1

I have a file that looks like:

chr1    1197592 .   C   A   .   .   DP=67;ECNT=1;NLOD=8.12  GT:AD:AF:F1R2   0/1:37,2:0.063:13,0
chr1    1355707 .   G   T   .   .   DP=69;ECNT=1;NLOD=4.51  GT:AD:AF:F1R2   0/1:50,3:0.059:20,3
chr1    1641723 .   TC  T   .   .   DP=59;ECNT=1;NLOD=2.40  GT:AD:AF:F1R2   0/1:30,2:0.089:12,4
chr1    2550056 .   TC  CT  .   .   DP=99;ECNT=1;NLOD=9.03  GT:AD:AF:F1R2   0/1:63,2:0.053:33,2

I would like to print the lines for which the 3rd value in the 10th column is greater than 0.06.

cat file.txt | cut -f 10 | cut -f 3 | awk -F':' '$3>0.06'

Will only give me:

0/1:37,2:0.063:13,0
0/1:30,2:0.089:12,4

And I would like the entire line. Can that be done with awk?

  • Since you're working with VCF files, an extremely complex format whose variety is not represented in this limited example, you might want to consider our sister site Bioinformatics for future questions, since the users there will be much more aware of the details of this format and how to deal with it. – terdon Jan 8 at 11:54
  • Thanks @terdon, i will consider it next time! – lindak Jan 8 at 12:15
3

As soon as you start discarding data in a pipeline (which is what cut does), you can't get it back in a later stage of the same pipeline.

Instead,

$ awk 'split($10,a,":") && a[3] > 0.06' file
chr1    1197592 .   C   A   .   .   DP=67;ECNT=1;NLOD=8.12  GT:AD:AF:F1R2   0/1:37,2:0.063:13,0
chr1    1641723 .   TC  T   .   .   DP=59;ECNT=1;NLOD=2.40  GT:AD:AF:F1R2   0/1:30,2:0.089:12,4

This takes the 10th whitespace-delimited field and splits it on colons into the array a. It prints the original line if the split() generated any elements in a and if the third element of a is greater than 0.06.

If the fields in the file are tab-delimited and if any field contains spaces, then use -F '\t' with awk to make sure that it finds the right fields correctly (the given example data does not have any such issues as far as I can see).


Addressing the follow-up question in comments:

awk 'split($10,a,":") && a[3] > 0.06 && split($11,b,":") && b[3] > 0.01' file
  • Will it work also if i have a column 11 with similar info as in 10 to filter on them both simultaneously? E.g. to print only lines where value 3 in column 10>0.06 and value 3 in column 11>0.01. Something like: awk '(split($10,a,":") && a[3] > 0.06) && (split($11,b,":") && b[3] > 0.01)' – lindak Jan 8 at 15:09
  • @lindak Yes, but the parentheses are optional there (unless you later want to change to (... && ...) || (... && ...)). See updated answer. – Kusalananda Jan 8 at 15:11
3
awk -F':' '$(NF-1) > 0.06 {print $0}' file

Just use ':' as separator and evaluate the penultimate column

  • Thank you, it works for my example file, but my actual file has more columns which i never commented in the post, so my bad... – lindak Jan 8 at 12:09
0

Having read the comments it's clear that your sample data doesn't match your actual data (and it's using spaces instead of TABs to begin with, which means that even the command output of your own device won't work), but having clobbered together a different input file like so:

cat file.txt
chr1    1197592 .       C       A       .       .       DP=67;ECNT=1;NLOD=8.12  GT:AD:AF:F1R2   0/1:37,2:0.063:13,0     0/1:37,2:0.0005:13,0
chr1    1355707 .       G       T       .       .       DP=69;ECNT=1;NLOD=4.51  GT:AD:AF:F1R2   0/1:50,3:0.059:20,3     0/1:50,3:0.0005:20,3
chr1    1641723 .       TC      T       .       .       DP=59;ECNT=1;NLOD=2.40  GT:AD:AF:F1R2   0/1:30,2:0.089:12,4     0/1:30,2:0.0015:12,4
chr1    2550056 .       TC      CT      .       .       DP=99;ECNT=1;NLOD=9.03  GT:AD:AF:F1R2   0/1:63,2:0.053:33,2     0/1:63,2:0.0005:33,2

This will then output all lines matching both conditions:

awk -F"[  :]" '$15>0.06 && $19>0.001' file.txt
chr1    1641723 .   TC  T   .   .   DP=59;ECNT=1;NLOD=2.40  GT:AD:AF:F1R2   0/1:30,2:0.089:12,4 0/1:30,2:0.0015:12,4
-1

I have used below method to achieve the result

for  i in `awk  '{print $NF}' file.txt | awk -F ":" '$3>"0.06"{print $0}'`; do awk -v i="$i" '$NF ==i{print $0}' file.txt ;done

output

chr1    1197592 .   C   A   .   .   DP=67;ECNT=1;NLOD=8.12  GT:AD:AF:F1R2   0/1:37,2:0.063:13,0
chr1    1641723 .   TC  T   .   .   DP=59;ECNT=1;NLOD=2.40  GT:AD:AF:F1R2   0/1:30,2:0.089:12,4
[root@praveen_linux_example ~]#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.