It seems hard to believe that your initial problem couldn't have been solved with some combination of pv, a while loop and wc.