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
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.
Login before adding your answer.
Use of this site constitutes acceptance of our User Agreement and Privacy