Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

how to convert comma delimited file to tab separator

Shell Programming and Scripting


Tags
convert comma file to tab separator

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-21-2012   -   Original Discussion by krupasindhu18
krupasindhu18's Unix or Linux Image
krupasindhu18 krupasindhu18 is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 9 April 2015, 8:18 AM EDT
Location: Chennai,India
Posts: 76
Thanks: 13
Thanked 1 Time in 1 Post
how to convert comma delimited file to tab separator

Hi all,

How can i convert comma delimited .csv file to tab separate using sed command or script.

Thanks,
Krupa
Sponsored Links
    #2  
Old Unix and Linux 02-21-2012   -   Original Discussion by krupasindhu18
rangarasan's Unix or Linux Image
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 11 December 2017, 4:20 AM EST
Location: Chennai, India
Posts: 509
Thanks: 16
Thanked 131 Times in 127 Posts
Hi,
Try this,

Code:
sed 's/\,/\t/g' file >outfile
awk '{gsub(/\,/,"press tab");print;}' file >outfile

if you dont want use the new file to store the result and update in same file. just use the below cmd

Code:
awk '{gsub(/\,/,"press tab");a[NR]=$0;}END{for (i=1;i<=NR;i++){print a[i] >FILENAME;}}' file

press tab button in gsub replace str.
Cheers,
Ranga:-)

Last edited by rangarasan; 02-21-2012 at 03:08 PM.. Reason: added additional solution
Sponsored Links
    #3  
Old Unix and Linux 02-21-2012   -   Original Discussion by krupasindhu18
methyl's Unix or Linux Image
methyl methyl is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 8 June 2016, 9:58 PM EDT
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 Posts

Code:
cat oldfile.txt | tr '[,]' '[\t]' > newfile.txt

    #4  
Old Unix and Linux 02-21-2012   -   Original Discussion by krupasindhu18
alister's Unix or Linux Image
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 977 Times in 791 Posts
Quote:
Originally Posted by methyl View Post

Code:
cat oldfile.txt | tr '[,]' '[\t]' > newfile.txt

Why the square brackets? If those are intended to be single character bracket expressions, tr does not support that syntax. Well, I should say, POSIX tr. Perhaps that is an older dialect? In modern implementations, that simply replaces [ with itself and ] with itself, a couple of no-ops, in addition to comma with tab.

Or am I overlooking something?

Regards,
Alister
Sponsored Links
    #5  
Old Unix and Linux 02-22-2012   -   Original Discussion by krupasindhu18
methyl's Unix or Linux Image
methyl methyl is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 8 June 2016, 9:58 PM EDT
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 Posts
Quote:
Or am I overlooking something?
Obviously harmless if the square brackets are both sides. I had never noticed that unescaped [] were not treated as special characters in some syntaxes of Posix "tr".
Strangely I don't usually put [] for single characters but it ended up like that after simplifying the command.

The "tr" command line modifies the default translation table for that locale. There is no additional processing when the translate itself takes place.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to convert a space delimited file into a pipe delimited file using shellscript? nithins007 Shell Programming and Scripting 7 09-25-2011 06:33 AM
Urgent! need help! how to convert this file into comma delimited format natalie23 UNIX for Advanced & Expert Users 2 08-28-2009 05:39 AM
how to convert this file into comma delimited format natalie23 Shell Programming and Scripting 1 08-28-2009 05:25 AM
convert a pipe delimited file to a':" delimited file priyanka3006 Shell Programming and Scripting 6 05-26-2009 11:53 AM
Converting Tab delimited file to Comma delimited file in Unix charan81 Shell Programming and Scripting 22 01-20-2006 09:24 AM



All times are GMT -4. The time now is 10:14 PM.