Convert date column as yyyy/mm/dd format

Hi All,

I have file like

�April 10, 2013�,�raj�
�April 29, 2013�,�raj1�

Output :

�2013/04/10�,�raj�
�2013/04/29�,�raj1�

Please help me how to do...

What have you tried?

I am not get any idea..suggest me

With more than 250 posts in this forum, you should have some idea of how to do this by now. Please show us that after watching the discussions on various problems posted in these forums that you have learned something from us in a year and a half. We'll be happy to help if you're having problems getting a working solution, but you should be able to show us a good start at a solution to this problem.

Please also specify what type of system and what shell you're using (since date processing capabilities vary a lot from system to system).

However, I would STRONGLY suggest that you convert your input file to just use plain double quotes ( " ) rather than using one opening double quote ( ) and three closing double quotes ( ) per line in your input file and expected output file formats. I have never seen a comma separated values file that uses unmatched opening and closing double quotes for different comma separated values.

i tried with this one.. Now it's working ... can you suggest me how to do simply way..

 cat test.txt | awk -F "," '{
    month=substr($1,2,length($1))
    year=substr($2,1,length($2)-1)
    split(month,mon_date," ")    
    cmd ="date \"+%Y/%m/%d\" -d \"" mon_date[1] mon_date[2] year"\""
    cmd | getline var   
   printf("\"%s\",\"%s\"\n",var,$3)
    close(cmd)
}'  

Dont use cat.. awk can read the file directly :slight_smile:
Is it taking time? If not i dont see why you want to simplify it more?

Thanks.. Do you any idea.. how to do simply way

If you want a more portable script (that will work on systems whether or not the date utility supports the -d option), you could try something like:

awk -F'"' '
BEGIN { m["January"] = 1
        m["February"] = 2
        m["March"] = 3
        m["April"] = 4
        m["May"] = 5
        m["June"] = 6
        m["July"] = 7
        m["August"] = 8
        m["September"] = 9
        m["October"] = 10
        m["November"] = 11
        m["December"] = 12
}
{       if((n = split($2, dt, /,* /)) != 3) {
                printf("Date field <<%s>> not in expected format.\n", $2)
                next
        }
        printf("\"%s/%02d/%02d\",\"%s\"\n", dt[3], m[dt[1]], dt[2], $4)
}' test.txt

This script assumes that the contents of test.txt only contains regular double quote characters rather than a mixture of opening and closing double quotes.

(On a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of awk .)

Thank lot.. i will try..

---------- Post updated at 06:17 AM ---------- Previous update was at 02:29 AM ----------

@Don.. i tried with awk command not working.. it raising the below error..

 Date field <<>> not in expected format.
Date field <<>> not in expected format.  

I said:

To get the error messages above, my assumption was obviously incorrect.

Change the first line of the script from:

awk -F'"' '

to:

awk -F'[�"�]' '

and it should work with any mix of different types of double quote characters.