Skip to content

Instantly share code, notes, and snippets.

@crazyhottommy
Created August 25, 2015 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazyhottommy/85de6ed22efa541ada17 to your computer and use it in GitHub Desktop.
Save crazyhottommy/85de6ed22efa541ada17 to your computer and use it in GitHub Desktop.
## imagine we have a file with one line header, and we want to keep the header after sorting
## use subshells http://bash.cyberciti.biz/guide/What_is_a_Subshell%3F
(sed -n '1p' your_file; cat your_file | sed '1d' | sort) > sort_header.txt
## if you have two header lines and want to keep both of them:
(sed -n '1,2p' your_file; cat your_file | sed '1,2d' | sort) > sort_header.txt
## if you have many lines starting with "#" as header, like vcf files
(grep "^#" my_vcf; grep -v "^#" my_vcf | sort -k1,1V -k2,2n) > sorted.vcf
## one can also use awk
cat my_vcf | awk '$0~"^#" { print $0; next } { print $0 | "LC_ALL=C sort -k1,1V -k2,2n" }'
## I am a useless cat user :) http://stackoverflow.com/questions/11710552/useless-use-of-cat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment