Home Man
Search
Today's Posts
Register

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Date format conversion how to change this from using nawk to awk

Tags
solved

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 12-12-2017
Date format conversion how to change this from using nawk to awk

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:

Code:
$: 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.

Code:
 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.
# 2  
Old 12-13-2017
Hello newbie_01,

On a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk.

Thanks,
R. Singh
# 3  
Old 12-13-2017
Thanks. Tested using /usr/xpg4/bin/awk works fine.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash Scripting with date format conversion cumulus_255 Shell Programming and Scripting 1 05-27-2016 07:56 AM
Date format conversion JSKOBS Shell Programming and Scripting 6 12-12-2014 04:14 AM
Date conversion and Format Tomlight Shell Programming and Scripting 1 04-16-2014 11:03 PM
Date conversion from 24 hr format to 12 hr format vivek d r Shell Programming and Scripting 2 03-06-2013 02:06 AM
Military type format date/time conversion jambesh Shell Programming and Scripting 6 11-08-2011 03:17 PM
Using awk or nawk to convert epoch time to date format minigts Shell Programming and Scripting 5 10-27-2011 12:56 PM
Date format conversion davidtd Shell Programming and Scripting 3 10-25-2011 01:42 AM
Date conversion from Standard/given format to seconds/epoch bpaac Shell Programming and Scripting 6 07-18-2011 09:27 PM
date format conversion siva_nagarajan UNIX for Dummies Questions & Answers 3 05-25-2010 02:41 AM
Date format conversion function Cecile UNIX for Dummies Questions & Answers 9 08-31-2006 09:46 AM


All times are GMT -4. The time now is 01:44 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password