I give up. Here's a perl script that does what I've been trying to do this whole time.