I don't really understand the logic behind both scripts, why do you have the $i operator at all? You're iterating over all columns of all rows, but you need to look only at DP?
Here's an example samtools-generated mpileup line with a depth of 4:
1 152715 T 4 ,,.. EEEA
I can't test Section 2 on that, I get a syntax error for that.
To filter by read-depth, all you have to do is
awk '{if ($4 > 5) print}' input > output
Here's the same for bcftools:
Here's an example mpileup line just made with bcftools mpileup v1.15, a small indel where reference A is replaced by AC
1 157103 . A AC 0 . INDEL;IDV=4;IMF=1;DP=4;I16=0,0,3,1,0,0,160,6400,0,0,168,7056,0,0,95,2275;QS=0,1;VDB=0.014407;SGB=-0.556411;MQSBZ=0;FS=0;MQ0F=0 PL 140,12,0
So we have four reads (DP=4), where it's easiest to use bcftools directly:
bcftools view -i'DP>5' input.mpileup > filteredoutput.mpileup