Results 1 to 6 of 6
I'm trying to write a bash script that has to extract values from a csv file. Problem is there are lines like this:
a,b,c,"dd,dd,dd",e,f,g
I'm using awk to extract the ...
- 11-04-2010 #1Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
[SOLVED] Ignoring commas within double quotes
I'm trying to write a bash script that has to extract values from a csv file. Problem is there are lines like this:
a,b,c,"dd,dd,dd",e,f,g
I'm using awk to extract the values but when I try it extract value 4 with awk I get:
"dd
instead of:
"dd,dd,dd"
Does anyone know how to get awk to ignore commas within double quotes?
- 11-04-2010 #2
Try this:
file: testfile
a,b,c,"dd,dd,dd",e,f,g
h,i,j,k,l,"mm,mm,mm",n
Edit: I just reread your post after posting, do you want to just nuke the quotes or make it look like dddddd or dd,dd,dd???Code:cat ./testfile | gawk '{ if($0~/^.*".*".*$/) { gsub(/"/,"",$0); print $0 } else { print $0 } }'
Output:
Code:a,b,c,dd,dd,dd,e,f,g h,i,j,k,l,mm,mm,mm,n
Last edited by barriehie; 11-04-2010 at 05:42 AM. Reason: Oops
- 11-04-2010 #3Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
Thanks for the reply. Right now if I say:
The output is "ddCode:awk '{print $4}'
I want the output to be "dd,dd,dd"
Ideally I would like the double quotes removed but I can do that later with sed if I have to.
My knowledge of awk if very limited.
- 11-04-2010 #4
- 11-04-2010 #5Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
You can do something like this:
Code:awk -F\" '{for(i=1;i<=NF;i+=2) {gsub(",", ";", $i)}}1' OFS= file.csv | awk -F\; '{ print $1;print $2;print $3;print $4;print $5}'
- 11-04-2010 #6Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
Thanks Franklin52, that works.



