How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below.

Current file ( file.csv )
----------------------

 empname,date_of_join,dept,date_of_resignation
ram,08/09/2015,sales,21/06/2016
"akash,sahu",08/10/2015,IT,21/07/2016

Required output file
----------------------

 empname,date_of_join,dept,date_of_resignation
ram,2015-09-08,sales,2016-06-21
"akash,sahu",2015-10-08,IT,2016-07-21

We are using below script for this. This script works fine for first record, but it does not give correct output for second record. Please see the actual output and script below-

Awk Script (This is .ksh file)
-------------------------------

 #!/bin/ksh
awk 'BEGIN{ FS=OFS="," 
            nf=split("2,4", f)                           # array of input field numbers
            nd=split(",7,4,-,4,2,-,1,2", d)        # array of date subfield info (in output order): prefix(out),pos(in),len(in)         
     }
     { for(i=1; i<=nf; i++){ 
           if($f) {
               fmod=""
               for(j=1; j<=nd; j+=3) fmod=fmod sprintf("%s", d[j] substr($f, d[j+1], d[j+2]))
               $f = fmod
           }
       } print
     }' file.csv

Actual Output which is not correct
--------------------------------------

 ram,2015-09-08,sales,2016-06-21
"akash,-u"-sa,08/10/2015,--IT,21/07/2016

Please suggest me, how Awk can handle the second record which contain comma inside a field?

if you have gawk, then you can use FPAT variable to split the data.

The GNU Awk User�s Guide

How about

awk -F, -vOFS=, '{for (i=1; i<=NF; i++) {split ($i, T, "/"); if (T[1] < 32 && T[2] < 13 && T[3] > 2000) $i = T[3] "-" T[2] "-" T[1]}} 1' file
empname,date_of_join,dept,date_of_resignation
ram,2015-09-08,sales,2016-06-21
"akash,sahu",2015-10-08,IT,2016-07-21

Hi Rudi,

Can you please tell me, on which version you have tried? Since I am working in Solaris, it is not working for me.

Where and how does it fail? Any error message? Wrong output?
It works here on linux and FreeBSD.

And, of course:

Hi Rudi,

On Solaris, awk is giving below error message-

awk: syntax error near line 1
awk: bailing out near line 1

but when I am tried with nawk, it is giving output but removing all commas like below-

ram 2015-09-08 sales 2016-06-21
"akash sahu" 2015-10-08 IT 2016-07-21

Here date conversion is working fine but I want comma also between the fields. Please see the below required output-

ram,2015-09-08,sales,2016-06-21
"akash,sahu",2015-10-08,IT,2016-07-21

Try a little modification:

-v OFS=","
1 Like