|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
|
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
Borrowing from Scrutinizer's idea... ![]() Code:
awk 'NR%2{gsub(/,/,"|")}1;END{printf("\b")}' RS='"' ORS='"' infileThe 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
|
|||
|
|||
|
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
|
||||
|
||||
|
Have you seen my post???
|
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|