perl -lane 'print unless (m/^#/)' < file.vcf > noheader.vcf
• 0 views
•
link
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.
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.
Log in to answer this question.