Biostar Beta. Not for public use.
Question: Remove VCF header lines
7
Entering edit mode

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.

ADD COMMENTlink 7.6 years ago win • 810 • updated 12 months ago zx8754 7.5k
14
Entering edit mode

Using negative matching:

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

egrep -v "^#" original.vcf > no_header.vcf
ADD COMMENTlink 7.6 years ago Matt Shirley 9.0k • updated 12 months ago zx8754 7.5k
6
Entering edit mode

Using sed

sed '/^#/d' your.vcf > noheader.vcf
ADD COMMENTlink 7.6 years ago Wen.Huang ♦ 1.2k • updated 12 months ago zx8754 7.5k
3
Entering edit mode

Using awk:

awk '! /\#/' variants.VCF > no_header.VCF
ADD COMMENTlink 7.6 years ago Vivek ♦ 2.3k • updated 12 months ago zx8754 7.5k
0
Entering edit mode

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 COMMENTlink 7.6 years ago Alex Paciorkowski 3.3k
Entering edit mode
2
perl -lane 'print unless (m/^#/)' < file.vcf > noheader.vcf
ADD REPLYlink 7.6 years ago
JC
7.9k
Entering edit mode
0

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

ADD REPLYlink 7.6 years ago
Alex Paciorkowski
3.3k
Entering edit mode
0

easy to apply and easy to forget ...

ADD REPLYlink 7.6 years ago
JC
7.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.0