Summary: Let's rewrite these stuff in Awk and Perl!
Modify the Awk and Perl versions to handle prefixes of any length. Experiment to determine what effect this changes has on performances.
Answer: I am going to defer this exercise, until I get myself more familiar with Awk and Perl.