In my latest project, an android app I’m developing as thesis for the Android App Development Course at the Politecnico of Milano, I came to encounter a tsv file.
Since I know I can easily convert a csv into and xml or json and “do things” with them, my first question was:
How do you convert tsv to csv?
I’ll admit it, it’s not something that I do on a daily basis.
So, after searching around the web and combing through google and github, I finally succeeded thanks to my old terminal friend.
This is the simple script that I wrote which got the job done:
LC_ALL=C cat inputfile.tsv | LC_ALL=C tr "\\t" "," > outputfile.csv
Piece of cake.
Even if the file was 400 MB, it didn’t take too long to process it.
Since in the tsv file the values are separated by a tab and in the csv by a comma (hence the names), I simply use cat to load the file and then pipe it into the tr unix utility to search and replace the tabs with the commas.
From the shell manual pages:
“The tr utility copies the standard input to the standard output with substitution or deletion of selected characters”.
Then I output the calculation to a file.
For more info on why the LC_ALL=C is needed, have a look here: