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?