I would like to extract all lines starting with a 1/1 in "12345" column and put the lines in a new file (-> Newfile.vcf.gz) leaving the first file.vcf.gz intact by using awk command. My file is a file.vcf.gz, please tell me if i need to convert it to something else first.


#CHROM      POS         ALT     12345   
1           345632      T       0/1:4,4:8:99:105,0,106
4           032184      C       1/1:46,9:55:99:99,0,1222
6           843290      A       0/1:67,20:87:99:336,0,1641

expected result

4           032184      C       1/1:46,9:55:99:99,0,1222

In awk you can use regular expressions. /^1\/1/ will match if column 4 starts with 1/1:

zcat file.vcf.gz | awk '$4 ~ /^1\/1/ {print}' | gzip -c > new.vcf.gz

But if / only can occur in column 4 why not just use grep?

zgrep "1/1" file.vcf.gz | gzip  -c > new.vcf.gz
|improve this answer|||||
  • I deleted my answer, yours is based on the same awk treatment but was more complete. – Valentin B. Nov 23 '16 at 14:25
  • 1
    Would be better as gunzip < file.vcf.gz instead of zcat < file.vcf.gz as on some systems, zcat only deals with .Z (compress-compressed) files (same for gunzip < file | grep vs zgrep). Note that -c is not needed when gzip is not passed any file argument (but does not harm. Same for {print} which is the default action in awk). – Stéphane Chazelas Nov 23 '16 at 14:34
  • 2
    Note that grep "1/1" would match on 21/1 and 1/12. The awk one would match on 1/12 as well, the OP may need /^1\/1:/ – Stéphane Chazelas Nov 23 '16 at 14:37
  • Oh thanks all! @hschou it worked! What a great first experience of asking for som bioinfo help. Nice! So - if I want to combined two and not only have 1/1. E.g. if I want all lines with 0/1 OR 1/0 from a column, what would that be? – Sanna Nov 23 '16 at 14:43
  • @Sanna grep allows "OR" via \|, so the match pattern would be 1/0\|0/1. (assuming no other matches as @stéphane-chazelas warned) – Fiximan Nov 23 '16 at 15:21

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.