Extract end position from CIGAR
The start position is the first correctly mapped base. One way to do it, is to use GenomicAlignments in R to read in the alignments.

For example the sam file looks like this:

cat test.sam 
@HD VN:1.3  SO:coordinate
@SQ SN:chrI LN:228861
D00689:415:CD4LUANXX:3:1316:7447:98083#CGGCTATGCAGGACGT 0   chrI    769 0   42M8S   *   0   0   CCATAGCCCGGTGGGGCCTGTGCTGCCACAGAGGGTGTGTCTAGATCGGA  B/B//FFFFFFBFFFF//7FF/&lt;F/FFBFFFF//&lt;F///&lt;F/BFB//&lt;F/  NM:i:1  MD:Z:21A20  AS:i:37 XS:i:37
D00689:415:CD4LUANXX:3:2308:9597:30445#CGGCTATGCAGGACGT 0   chrI    3965    0   3S47M   *   0   0   CCACACACCCACACACCACACCCACACCACACCCACACCACACCCACACC  BBBBBFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFFBF&lt;FFFFF&lt;FF  NM:i:0  MD:Z:47 AS:i:47 XS:i:47


We convert to bam:

samtools view -bS test.sam &gt; test.bam


And read using R:

library(GenomicAlignments)
gr = readGAlignments("test.bam")
gr

GAlignments object with 2 alignments and 0 metadata columns:
      seqnames strand       cigar    qwidth     start       end     width
         &lt;Rle&gt;  &lt;Rle&gt; &lt;character&gt; &lt;integer&gt; &lt;integer&gt; &lt;integer&gt; &lt;integer&gt;
  [1]     chrI      +       42M8S        50       769       810        42
  [2]     chrI      +       3S47M        50      3965      4011        47
          njunc
      &lt;integer&gt;
  [1]         0
  [2]         0
  -------
  seqinfo: 1 sequence from an unspecified genome


The start and the end can be retrieved using:

start(gr)
[1]  769 3965
end(gr)
[1]  810 4011