LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Convert CSV to Tab (https://www.linuxquestions.org/questions/programming-9/convert-csv-to-tab-683201/)

imkornhulio 11-13-2008 01:16 PM

Convert CSV to Tab
 
I want to convert a file from Comma Separated Value to Tab Separated Value
How can i do that in shell

file looks like this

name,accountid,address,city,state

I want it to displayed like

name accountid address city state

Please help!!!

rayfordj 11-13-2008 01:24 PM

something like?
Code:

sed -e 's/,/\t/g' /tmp/file.csv > /tmp/file.tabbed

H_TeXMeX_H 11-13-2008 01:25 PM

Easiest way is probably:

Code:

sed 's|,|\t|g' infile > outfile
EDIT:
rayfordj beat me to it ...

imkornhulio 11-13-2008 01:44 PM

it works

Thanks Guys

gmbastos 11-14-2008 01:10 PM

Also add this to the list:

cat filename.csv | tr "," "\t"

If you wish to have the results saved to a file instead of displayed, simply redirect the output to a file:

cat filename.csv | tr "," "\t" > newfile

Have fun! :D

Telemachos 11-14-2008 07:36 PM

Unnecessary cat. You can just do this:
Code:

< filename.csv tr "," "\t"
Or you could save with this:
Code:

< filename.csv tr "," "\t" > tabfile

gmbastos 11-15-2008 05:32 PM

Quote:

Originally Posted by Telemachos (Post 3342308)
Unnecessary cat. You can just do this:
Code:

< filename.csv tr "," "\t"
Or you could save with this:
Code:

< filename.csv tr "," "\t" > tabfile

Sure. Redirection works two-way.

I just would feel more comfortable using it after command. Just like:
Code:

tr "," "\t" < filename.csv > newfile
I prefer to use the cat command, anyway: I feel more comfortable this way. At least for this case. :)

Telemachos 11-15-2008 07:20 PM

Quote:

Originally Posted by gmbastos (Post 3343166)
Sure. Redirection works two-way.

I just would feel more comfortable using it after command. Just like:
Code:

tr "," "\t" < filename.csv > newfile
I prefer to use the cat command, anyway: I feel more comfortable this way. At least for this case.

Ok, well, you're entitled to your opinion, but I think it might be worth changing your habits.

http://sial.org/howto/shell/useless-cat/

That page also explains why I put the STDIN redirection first.


All times are GMT -5. The time now is 04:38 PM.