I am processing a CSV file with GAWK that has a field with a timestamp formatted like this "18-APR-22 11:00:00". I would like to format the date like this in the output of awk "2022-04-18 11:00:00". I was doing this with the "date -d" and getline. As in the following example.
awk -v FS="," -v OFS="," '
{
tmp = "date -d \""$1"\" +\"%F %T\""
tmp | getline var
close(tmp)
}
{split(FILENAME, arr, ".")}
{print NR, arr[1], tmp, $4, $7, $8, $9}
' 13003.ARR > test.csv
While this works it is very slow with large files. Is there a better way to take the timestamp field and change its format?
date
every time you want to convert "18-APR-22" to "2022-04-18" is a suboptimal method. Treat it as a simple string manipulation. Split the string on the-
s. Then,18
->18
is easy, as is22
->2022
.APR
->04
is table lookup awaygawk
manual, but as it states,mawk
has also incorporated theTime functions
.