I strongly recommend trying it on a tiny BAM file (e.g. with 2 reads).

Be aware that deleting every header with the letter M or any underscore (since `[0-9]*` happily matches nothing) is likely to delete more headers than you intended to.

Note that since BAM (not SAM) uses reference contig index, not reference contig name, `samtools reheader` gives incorrect results if you drop any `@SQ` headers that aren't at the end of the `@SQ` block. For example:
```
@SQ chr1
@SQ chr2
@SQ chrUn_decoy
```
to
```
@SQ 1
@SQ 2
```
is ok.

Both
```
@SQ chr1
@SQ chrUn_decoy
@SQ chr2
```
to
```
@SQ 1
@SQ 2
```
doesn't work at all with `reheader` because it's equivalent to dropping chr2 and renamed chrUn_decoy to chr2. It does *not* rename chr2 to 2.

Edit: when I tried your command on one of my bams I got:
```
[W::sam_hdr_link_pg] PG line with PN:samtools has a PP link to missing program 'bwa'
```
This was a result of your `sed` deleting one of the `@PG` headers.



Source link