Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-07-2012
Registered User
 
Join Date: Mar 2006
Posts: 97
Thanks: 0
Thanked 1 Time in 1 Post
Convert csv to pipe delimited except the ones in double quotes

I have a csv data file :

Code:
A,B,C,D,"A,B",E,"GG,H"
E,F,G,H,I,J,"S,P"

I need to replace all "," with "|" except the ones between double quotes i.e

Code:
A|B|C|D|"A,B"|E|"GG,H"
E|F|G|H|I|J|"S,P"

CAn someone assist?

Last edited by Franklin52; 08-07-2012 at 03:10 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old 08-07-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts
http://www.unix.com/shell-programmin...ited-file.html
Sponsored Links
    #3  
Old 08-07-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts
Borrowing from Scrutinizer's idea...

Code:
awk 'NR%2{gsub(/,/,"|")}1;END{printf("\b")}' RS='"' ORS='"' infile

The action corresponding to the END pattern will take care of the last ORS (extra).

Last edited by elixir_sinari; 08-08-2012 at 03:21 AM..
    #4  
Old 08-07-2012
Registered User
 
Join Date: Mar 2006
Posts: 97
Thanks: 0
Thanked 1 Time in 1 Post
The post closes there with no result on awk.

Only the perl approach works, I am looking out for awk approach


Code:
perl -MText::ParseWords -nle'
  print join "|", parse_line(",",0, $_);
  ' infile

awk -F'"' '{gsub(/,/,"|",$1);gsub(/,/,"|",$3);} 1' yourFile

Sponsored Links
    #5  
Old 08-07-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts
Quote:
Originally Posted by Shivdatta View Post
The post closes there with no result on awk.
Have you seen my post???
Sponsored Links
    #6  
Old 08-07-2012
Registered User
 
Join Date: Mar 2006
Posts: 97
Thanks: 0
Thanked 1 Time in 1 Post
yes thanks, that works fine but w/ nawk, btw can you please explain what NR%2 is doing here and what does '1' at the end mean in gsub


Code:
$ cat dd
A,"B,B",C,D,"A,B",E,"GG,H"
"E,F",F,G,H,I,J,"S,P"

$ nawk 'NR%2{gsub(/,/,"|")}1' RS='"' ORS='"' dd
A|"B,B"|C|D|"A,B"|E|"GG,H"
"E,F"|F|G|H|I|J|"S,P"

Sponsored Links
    #7  
Old 08-07-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts
As you might have realised, the input (and also the output) record separator is a " . So, all odd-numbered records ( NR%2 ) are outside the double-quotes "realm" and all the , within these records need to be replaced with | . The other records do not need to be bothered about. The 1 at the end of the script prints all the records irrespective of the substitution.
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 a space delimited file into a pipe delimited file using shellscript? nithins007 Shell Programming and Scripting 7 09-25-2011 05:33 AM
Convert CSV file (with double quoted strings) to pipe delimited file Ram.Math Shell Programming and Scripting 8 08-26-2011 07:15 AM
unix command to insert double quotes in a delimited file Bachu UNIX for Dummies Questions & Answers 6 06-17-2010 05:48 AM
convert a pipe delimited file to a':" delimited file priyanka3006 Shell Programming and Scripting 6 05-26-2009 10:53 AM
Convert CSV file (with double quoted strings) to pipe delimited file lehmanbrothers Shell Programming and Scripting 11 02-13-2009 12:14 PM



All times are GMT -4. The time now is 06:25 PM.