Hi,
I have a file where I need to change the date format on the nth field from DD-MM-YYYY to YYYY-MM-DD so I can accurately sort the record by dates
From regex - Use sed or awk to fix date format - Stack Overflow, I found an example using nawk.
Test run as below:
$: cat xyz.txt
A 21-11-2017 03:18:37 TESTING 17-06-2017 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 11-07-2017 03:35:07 TESTING 17-06-2017 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 14-07-2017 11:22:26 TESTING 17-06-2017 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 21-07-2017 02:15:47 TESTING 17-06-2017 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 21-08-2017 02:16:07 TESTING 17-06-2017 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 21-09-2017 02:16:15 TESTING 17-06-2017 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 21-10-2017 03:17:36 TESTING 17-06-2017 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
$:
$: nawk '
> BEGIN { }
> { split($2, date, /-/)
> $2 = date[3] "-" date[2] "-" date[1]
> split($5, date, /-/)
> $5 = date[3] "-" date[2] "-" date[1]
> print $0
> }
> ' xyz.txt
A 2017-11-21 03:18:37 TESTING 2017-06-17 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 2017-07-11 03:35:07 TESTING 2017-06-17 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 2017-07-14 11:22:26 TESTING 2017-06-17 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 2017-07-21 02:15:47 TESTING 2017-06-17 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 2017-08-21 02:16:07 TESTING 2017-06-17 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 2017-09-21 02:16:15 TESTING 2017-06-17 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
I 2017-10-21 03:17:36 TESTING 2017-06-17 22:00:42 Accessed: 2,618,310,656 /tmp/abc.dbf
$:
Unfortunately, some of the Solaris servers, Solaris8, do not have nawk and using awk gives error below.
awk '
> BEGIN { }
> { split($2, date, /-/)
> $2 = date[3] "-" date[2] "-" date[1]
> split($5, date, /-/)
> $5 = date[3] "-" date[2] "-" date[1]
> print $0
> }
> ' xyz.txt
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 5
awk: illegal statement near line 5
May I know how to get what I am wanting to do to work using awk?
Please advise. Thanks.