This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to print the header of bed file and the lines that belong to chromosome 2

I have a bed file consists of many lines and I want to extract the header and the lines that belong to chromosome 2 only in another file

fastq bed_files fasta bash

2 answers

awk 'NR == 1 || ($1=="chr2")' lamina.bed 

If the BED file is sorted, here's a faster way:

$ cat <(head -1 regions.bed) <(bedextract chr2 regions.bed) > answer.bed

Generically, using awk to solve this will require reading through the entire file. This can be expensive for whole-genome scale files.

Log in to answer this question.