How to remove the digits after : sign in each line from a vcf file. Example:

0|0:0.81        0|1:0.81        0|0:0.76        0|0:0.83
0|1:0.99        0|0:0.03        0|0:0.83        0|0:0.2

The output should be:

0|0        0|1        0|0        0|0
0|1        0|0        0|0        0|0

I have tried the following, but it removes everything after first :

sed 's/[:].*$//' file.vcf 
  • If you remove everything but the genotype, you also have to remove the corresponding keys in the FORMAT column of the vcf file. Otherwise you have an invalid vcf file. Instead of manipulating your file with sed I strongly recommend bcftools. In your case just run bcftools annotate -x FORMAT input.vcf. More examples in the manual: htslib.org/doc/bcftools.html#annotate – finswimmer Jan 22 '19 at 19:45
  • Thanks for your comment. In fact, my goal was not to use the vcf file in its format anymore. My goal was to get the genotypes without the dosage notations. Apparently, your suggestion will do what I need as well using bcftools and it seems very helpful if one wants to use the vcf file for further calculation. – Adam Majdi Jan 28 '19 at 15:00

You want to delete from the :, and then as many characters that are digits or dots as possible directly after that:

$ sed 's/:[0-9.]*//g' file.vcf
0|0        0|1        0|0        0|0
0|1        0|0        0|0        0|0

You also need that g at the end to repeat the substitution for all matching substrings on each line.

Your command,

sed 's/[:].*$//' file.vcf 

would match the first : character, and then any number of characters to the end of the line. This is why you end up with


as the output.

The difference between this and my suggested expression is the . (which matches any single character), which I changed to [0-9.] to restrict the set of characters that we actually want to delete. We also can't anchor the pattern to the end of the line with $ since we need to delete things in the middle of the line as well. And the g at the end, but I already mentioned that.

|improve this answer|||||

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.