This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Remove VCF header lines

Hi all,

Is there a simple way to remove header lines from a vcf file.

Although I would like to retain the "original" VCF file as well. So ideally if results.vcf is my file then I want to create results-noheader.vcf as another file without the header lines.

Any way this can be done?

Thanks in advance.

vcf

5 answers

Using negative matching:

-v, --invert-match select non-matching lines

egrep -v "^#" original.vcf > no_header.vcf

Using sed

sed '/^#/d' your.vcf > noheader.vcf

Using bcftools (https://www.htslib.org/doc/bcftools.html#view):

bcftools view --no-header results.vcf > results-noheader.vcf

Using awk:

awk '! /\#/' variants.VCF > no_header.VCF

Perl version:

#!/usr/bin/perl
use strict; 
use warnings;
# no_headers.pl
# Reads a vcf file and removes unwanted headers
my $file = shift;
open my $F, $file;
LINE: while ($_=<$F>) {
next if /^##/; # This removes your headers
my @line = split /\t/;
print join(qq/\t/,@line);
}

Then you can do:

$ perl no_headers.pl your.vcf > no_headers.vcf

Of course, the one liner already submitted is quicker & better.

perl -lane 'print unless (m/^#/)' < file.vcf > noheader.vcf

easy to apply and easy to forget ...

Log in to answer this question.