CSVFix
- 2 minutes read - 382 wordsWhere I work, we spend some time working with CSV files. The most usual situation is doing scheduled imports, of referential data such as lists of users, contacts, and so on.
Recently I was doing some work on a system where someone had mis-specified the import files, and as such we ended up in a situation where the CSV file provided couldn’t be imported, as it didn’t have a unique key column in it. This fouled the import, as the version of the software that the customer has couldn’t do a composite key. There was another issue in that I also needed to specify a some default values for another import – essentially adding two columns to the end of a file.
Fortunately, a colleague came up with a new tool for my toolkit – a piece of MIT-licensed open-source software called “CSVFix”. It’s basically a swiss-army knife for CSV files. I used it to sort the problem with the file above.
I used it to add the extra columns that our software needed, so that the imports could proceed. In once case, I added two fixed values, and in the other, created an extra column as the composite key I needed. I even managed to optimise one of the imports by using CSVFix to generate difference files to import, thus meaning that rather than matching 145K rows, I could just pull in the handful that had changed, saving a considerable amount of processing time.
Of course, this kind of text processing is possible in UNIX, but not in Windows. CSVFix makes these things easy. Each operation above was just one pipelined command, with two or three invocations of CSVFix, with different parameters.
It’s available for Windows as a binary here. Alternatively, you can connect to the source code repo, and build for other systems. Check out the manual, and see if it’s useful for you.
I strongly suggest checking this out if you work with CSV or XML files, or indeed just like to know what’s out there to help you solve problems. It’s certainly saved me a lot of time and effort. All I have to do now is see if there’s a way I can claim a donation to the project on expenses…
Update: Employer reference deleted. See why.