I see the problem is already fixed, but here goes anyway...
I whipped up a small Perl script for this, I thought it would make a fun exercise (I haven't done Perl for some time). It became a lot bigger than I originally thought because I added some functionality to it. I wrote it in windows, so if you want to use it in Unix open it with nano or pico or something to change the line endings.
Just run the thing, it'll tell you what to do. I even tested the thing, so it shouldn't contain
too many bugs.
K.