Help with date in AIX

Hi there...

I have this perl command which convert an epoch timestamp into human readable date:

perl -i -lpe 's/([\d]{10})/localtime $1/eg;'

which is providing this output:

Mon May 1 00:25:28 2023

How can I modify the perl command (without using strftime) to format the date as "%Y-%0m-%0d HH:MM:SS"?

Thanks in advance!

I'm not really a perl guru, but:

$ perl -e '($ss, $mm, $hh, $DD, $MM, $YY) = localtime;  printf "%04d-%02d-%02d %02d:%02d:%02d\n", $YY + 1900 , $MM +1 , $DD, $hh, $mm, $ss'
2023-05-02 20:33:19

Or with AiX's ksh93:

$ echo $0
ksh93
$ printf "%(%Y-%m-%d %H:%M:%S)T\n" now
2023-05-02 20:37:11

And probably many-many more ways to do it...

2 Likes

Thanks, I've found this other way working on this limited Aix endpoint:

perl -i -MPOSIX -lpe 's/([\d]{10})/POSIX::strftime("%Y-%0m-%0d %T", localtime $1)/eg;' "$FILE"

I thought you said "(without using strftime)", but either way - congrats!

2 Likes

you are right, I did.. that's because when I've tried a command using strftime before I got an error message saying strftime was not found. Anyway, this one worked. Thanks.

Yes, strftime is from the POSIX module.
Without POSIX::strftime perl starts faster:

perl -i -lpe 's/([\d]{10})/{ ($ss, $mm, $hh, $DD, $MM, $YY) = localtime $1;  sprintf "%04d-%02d-%02d %02d:%02d:%02d", $YY + 1900 , $MM +1 , $DD, $hh, $mm, $ss; }/eg;' "$FILE"

Note: the g modifier looks for further matches in the current line and would substitute them. For example, a line
1683120629 123456789012 sometext
will become
2023-05-03 15:30:29 2009-02-14 01:31:3012 sometext

1 Like

This topic was automatically closed 300 days after the last reply. New replies are no longer allowed.