How to merge sam files on Linux?
According to the sam format specification, header lines start with
@, while alignment lines do not. So you can use
grep to merge sam files as follows efficiently.
Assume the header is from 0.sam, the files to be merged are 0.sam, 1.sam and 2.sam, and the merged output sam file is out.sam.
header=0.sam files="0.sam 1.sam 2.sam" output=out.sam (grep ^@ $header; for f in $files; do grep -v ^@ $f; done) > $output
It should be much faster than the way of “convert sams to bams, merge bams by
samtools merge, convert bam to sam”.