Need a script to convert comma delimited files to semi colon delimited | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Need a script to convert comma delimited files to semi colon delimited

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-14-2013
CarpKing CarpKing is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 14 February 2013, 6:00 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Need a script to convert comma delimited files to semi colon delimited

Hi All,

I need a unix script to convert .csv files to .skv files (changing a comma delimited file to a semi colon delimited file). I am a unix newbie and so don't know where to start. The script will be scheduled using cron and needs to convert each .csv file in a particular folder to a .skv equivalent and create a token in the folder to show completion as well as a log showing the number of files and records converted for auditing.

Sample data ... before conversion


Code:
27981,13,"0901"," ","B18975362",1,"C",17,"1"," "," "," "
24875,12,"0901"," ","B24578942",0,"F",17,"0"," "," "," "

Sample data ... after conversion


Code:
27981;13;0901;;B18975362;1;C;17;1;;;;^ 
24875;12;0901;;B24578942;0;F;17;0;;;;^

Please note that the number of columns in each file to be converted could be different, but that each .skv should have the same number of columns as the source .csv

example source & target folder: /test/conv

Any help would be most appreciated
Sponsored Links
    #2  
Old 02-14-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
try..


Code:
sed 's/,/;/g; s/\"//g; s/$/;^/' file

Sponsored Links
    #3  
Old 02-14-2013
CarpKing CarpKing is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 14 February 2013, 6:00 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Pamu,

Thanks for the suggestion ... as I mentioned I am a complete newbie and dont know how I would write the script at all. Please could you give me a more detailed example that I could test on my data?

Many thanks
    #4  
Old 02-14-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
You can do sth like below...


Code:
$ cat file
27981,13,"0901"," ","B18975362",1,"C",17,"1"," "," "," "
24875,12,"0901"," ","B24578942",0,"F",17,"0"," "," "," "

$ sed 's/,/;/g; s/\"//g; s/$/;^/' file > out_file

$ cat out_file
27981;13;0901; ;B18975362;1;C;17;1; ; ; ;^
24875;12;0901; ;B24578942;0;F;17;0; ; ; ;^

Hope this helps you

Regards,

pamu
Sponsored Links
    #5  
Old 02-14-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 September 2014, 11:34 PM EDT
Location: Amsterdam
Posts: 9,465
Thanks: 279
Thanked 2,388 Times in 2,140 Posts

Code:
awk 'NR%2{gsub(/,/,";")}1' RS=\" ORS= file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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



All times are GMT -4. The time now is 10:13 AM.