LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2008, 01:16 PM   #1
imkornhulio
Member
 
Registered: Aug 2008
Posts: 58

Rep: Reputation: 15
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!!!
 
Old 11-13-2008, 01:24 PM   #2
rayfordj
Member
 
Registered: Feb 2008
Location: Texas
Distribution: Fedora, RHEL, CentOS
Posts: 488

Rep: Reputation: 78
something like?
Code:
sed -e 's/,/\t/g' /tmp/file.csv > /tmp/file.tabbed
 
Old 11-13-2008, 01:25 PM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Easiest way is probably:

Code:
sed 's|,|\t|g' infile > outfile
EDIT:
rayfordj beat me to it ...
 
Old 11-13-2008, 01:44 PM   #4
imkornhulio
Member
 
Registered: Aug 2008
Posts: 58

Original Poster
Rep: Reputation: 15
it works

Thanks Guys
 
Old 11-14-2008, 01:10 PM   #5
gmbastos
LQ Newbie
 
Registered: Apr 2007
Location: Brazil
Distribution: Slackware
Posts: 21

Rep: Reputation: 15
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!

Last edited by gmbastos; 11-14-2008 at 01:12 PM. Reason: including filesaving
 
Old 11-14-2008, 07:36 PM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Unnecessary cat. You can just do this:
Code:
 < filename.csv tr "," "\t"
Or you could save with this:
Code:
 < filename.csv tr "," "\t" > tabfile
 
Old 11-15-2008, 05:32 PM   #7
gmbastos
LQ Newbie
 
Registered: Apr 2007
Location: Brazil
Distribution: Slackware
Posts: 21

Rep: Reputation: 15
Quote:
Originally Posted by Telemachos View Post
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.
 
Old 11-15-2008, 07:20 PM   #8
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by gmbastos View Post
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.

Last edited by Telemachos; 11-15-2008 at 07:41 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert a number on each line to CSV file OlRoy Programming 9 08-20-2008 11:05 AM
Convert into CSV file say_hi_ravi Programming 4 07-17-2008 03:25 AM
convert excel to csv and html thelonius Programming 3 03-28-2007 11:16 AM
Script to convert csv 2 xls or odt xowl Linux - Software 1 01-16-2007 09:06 PM
convert CSV (TEXT) files to UTF-16 cccc Programming 1 07-01-2004 01:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration