Unix/Linux Go Back    


UNIX for Beginners Questions & Answers 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

UNIX for Beginners Questions & Answers


Tags
solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-13-2017   -   Original Discussion by newbie_01
newbie_01's Unix or Linux Image
newbie_01 newbie_01 is offline
Registered User
 
Join Date: May 2009
Last Activity: 16 January 2018, 7:00 AM EST
Posts: 232
Thanks: 44
Thanked 1 Time in 1 Post
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.
Sponsored Links
    #2  
Old Unix and Linux 12-13-2017   -   Original Discussion by newbie_01
RavinderSingh13's Unix or Linux Image
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 17 January 2018, 10:46 AM EST
Location: Chennai
Posts: 2,689
Thanks: 594
Thanked 1,278 Times in 1,149 Posts
Hello newbie_01,

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

Thanks,
R. Singh
Sponsored Links
    #3  
Old Unix and Linux 12-13-2017   -   Original Discussion by newbie_01
newbie_01's Unix or Linux Image
newbie_01 newbie_01 is offline
Registered User
 
Join Date: May 2009
Last Activity: 16 January 2018, 7:00 AM EST
Posts: 232
Thanks: 44
Thanked 1 Time in 1 Post
Thanks. Tested using /usr/xpg4/bin/awk works fine.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Date format conversion JSKOBS Shell Programming and Scripting 6 12-12-2014 05:14 AM
Need to change date format in a csv file using awk adit Shell Programming and Scripting 8 12-11-2014 01:58 PM
awk - change date format vegasluxor Shell Programming and Scripting 8 01-25-2013 06:32 AM
Using awk or nawk to convert epoch time to date format minigts Shell Programming and Scripting 5 10-27-2011 01:56 PM
date format conversion siva_nagarajan UNIX for Dummies Questions & Answers 3 05-25-2010 03:41 AM



All times are GMT -4. The time now is 04:13 PM.