1

I've below file and need to convert its date format to YYYY-MM-DD by using awk. Sample content of File.TXT

1;ABC;SSS;Mar 12, 2020;4;1;0;5;0;0;0;0
2;DEF;AAA;Apr 14, 2020;4;1;0;5;0;0;0;0

Expecting output

1;ABC;SSS;2020-03-12;4;1;0;5;0;0;0;0
2;DEF;AAA;2020-04-14;4;1;0;5;0;0;0;0
Inian
  • 80,270
  • 14
  • 142
  • 161
dura
  • 11
  • 1
  • 1
    On SO we encourage users to show their efforts in form of code, so kindly do add the same in your question and let us know then. – RavinderSingh13 Apr 15 '20 at 06:45
  • Does this answer your question? [Formatting date strings in a file with linux bash shell](https://stackoverflow.com/questions/61015078/formatting-date-strings-in-a-file-with-linux-bash-shell) – Cyrus Apr 15 '20 at 07:49
  • I used this command and its not working .. cat File.txt | awk -F ';' '{OFS="|"} {print $(date -d $3 +"%Y%m%d")}' – dura Apr 15 '20 at 13:55

1 Answers1

0

This script should do the trick:

BEGIN{
    FS=";"
    OFS=";"
    month["Jan"] = "01"
    month["Feb"] = "02"
    month["Mar"] = "03"
    month["Apr"] = "04"
    month["May"] = "05"
    month["Jun"] = "06"
    month["Jul"] = "07"
    month["Aug"] = "08"
    month["Sep"] = "09"
    month["Oct"] = "10"
    month["Nov"] = "11"
    month["Dec"] = "12"
}
{
    split($4,date," ")
    $4 = date[3]"-"gsub(",","",date[2])"-"month[date[1]]
    print
}

call with awk -f script.awk youinput.txt

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44