https://github.com/taltman/scripts/blob/master/EDA/samp
It's shorter than this ruby implementation, and includes copious documentation, file handling, and corner-case support.