Remove VCF header lines
5
7
Entering edit mode
11.7 years ago
win ▴ 970

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 • 22k views
ADD COMMENT
15
Entering edit mode
11.7 years ago

Using negative matching:

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

egrep -v "^#" original.vcf > no_header.vcf
ADD COMMENT
7
Entering edit mode
11.7 years ago
Wen.Huang ★ 1.2k

Using sed

sed '/^#/d' your.vcf > noheader.vcf
ADD COMMENT
3
Entering edit mode
11.7 years ago
Vivek ★ 2.7k

Using awk:

awk '! /\#/' variants.VCF > no_header.VCF
ADD COMMENT
3
Entering edit mode
3.7 years ago
hackdna ▴ 30

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

bcftools view --no-header results.vcf > results-noheader.vcf
ADD COMMENT
0
Entering edit mode
11.7 years ago

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.

ADD COMMENT
2
Entering edit mode
perl -lane 'print unless (m/^#/)' < file.vcf > noheader.vcf
ADD REPLY
0
Entering edit mode

Oh sure...everyone wants one-liners... ;)

ADD REPLY
0
Entering edit mode

easy to apply and easy to forget ...

ADD REPLY

Login before adding your answer.

Traffic: 1850 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6