Convert TSV to CSV – OSX Shell

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:

Piece of cake.

Even if the file was 400 MB, it didn’t take too long to process it.

Little explanation:
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:
http://unix.stackexchange.com/questions/141420/tr-complains-of-illegal-byte-sequence

Lascia una risposta

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *

Immagine CAPTCHA

*

È possibile utilizzare questi tag ed attributi XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">