Sorting BED files with headers.

I’m trying to keep most of my genome stats in bed format. This makes it pretty easy to mix and match analyses. However, one task that seems to come up over and over is how to sort the files when they have a header. So I don’t have to remember it and you don’t have to spend anymore time googling it the easy ‘linux’ way to do this is with the following one-liner:

cat  file.wheader.bed \
| { head -n 1; tail -n +2 \
| sort -k1,1 -k2,2g; } \
> file.wheader.sorted.bed


Leave a Reply

Your email address will not be published. Required fields are marked *