awk -File contain double quotes

Hi ALL,

file data like :
test.csv

a,b,"c,d"

my awk version is 4.0.2 ,if i am using the below code is working fine.

awk -vFPAT='([^,]*)|("[^"]+")' -vOFS="," '{print $3}' test.csv

if the awk version is 3.1.7 is not working . Could you please help me on this one.

output should be : "c,d"

You could look at installing csvtool or csvkit from your Linux Repository (assuming Linux here as you did not mention your OS). The former is a single program; the latter is a suite of utilities.

Both should be able to pull a single column from a CSV file (by column heading if you have one!) for you to manipulate without the confusion of quotes or even embedded linefeeds.

Andrew

Try -F'([^,]*)|("[^"]+")' instead of -vFPAT.

thanks for reply..
i tried below and it is not displaying the values ...

 awk -F '([^,]*)|("[^"]+")'  -vOFS=, '{print $1}' test.csv

Can we have another solution like ...
if the delimeter is comma,replace with pipe ,within double quote comm don't distrub.

output should be :

a|b|"c,d"

THANKS

Can you do that? Fantastic, problem solved.

If your data's already broken though, not really a solution.

The quoting of fields cannot be handled by looking at the separator. All attempts with FS or -F fail.
But replacing a , with a | outside the quotes can be done with another awk

awk 'BEGIN{FS=OFS="\""} {for (i=1; i<=NF; i+=2) gsub(/,/, "|", $i); print}'
1 Like