For each one of the 500 bins, we use the intersectBed program of the bedtools utilities to compare the bins.bed file and meth.bedGraph files base-by-base. The output is piped into groupBy to calculate the number of features being joined (in this case is the methylation values at CpG) and the average methylation level for each bin (i.e. -c 7,7 -o count,mean). Lastly, awk was used to print the methylation levels in four decimal places.