Hi guys, 
I have a folder with around 300 `.bam` files. Each `.bam` file is a lane of a sample and hence 4 lanes make a sample. I would like to merge the .bam files of the four lanes in a single one by `_*S*_` where S is followed by a number that represent the number of the sample (e.g. `my_experimet_xxx__L001_S1_stimulated_Aligned.bam`). 
Suppose I have 75 samples, i.e. `${1...75}`. 

Can anyone help me please? 

The line I use to merge normally is the following: 

    samtools merge S1_merged.bam *bam

Thank you in advance 



I think this should work for your issue:

create a file name samtools_merge.sh

    $mkdir merged

    for L1 in *_L001_*.bam
    do
        echo $L1
        L2=`echo $L1 | sed 's/_L001_/_L002_/'`
        L3=`echo $L1 | sed 's/_L001_/_L003_/'`
        L4=`echo $L1 | sed 's/_L001_/_L004_/'`
        merged=`echo $L1 | sed 's/_L001_/_merged_/'`
        samtools merge ./merged/${merged} ${L1} ${L2} ${L3} ${L4}
    done

This will iterate over each unique sample with L001 somewhere in the name and store other variables by replacing L001 with L002,003,004, and do this for each sample. Then it will run samtools merge on all 4 lanes, then do the same for the next sample until it has gone through all the samples. The filename output will be the same as the sample name, but will substitute lane information with "merged". 

Just run this in your directory with all the bams and you should have merged bams in the dir "merged".