I have a file containing this:
1415602803,LOGIN SUCCESS,AUTH,user2,192.168.203.63,10.146.124.73,59996,22
1415602807,LOGIN SUCCESS,AUTH,user1,172.24.31.10,172.32.1.1,48191,22
1415602811,LOGIN FAILED,AUTH,root,172.24.166.153,10.146.124.73,52506,22
1415602815,LOGIN FAILED,AUTH,user3,192.168.123.55,10.146.32.99,55750,22
I want to convert the timestamp to a date in this format:
2014-11-10 02:00:03,LOGIN SUCCESS,AUTH,user2,192.168.203.63,10.146.124.73,59996,22
2014-11-10 02:00:07,LOGIN SUCCESS,AUTH,user1,172.24.31.10,172.32.1.1,48191,22
2014-11-10 02:00:11,LOGIN FAILED,AUTH,root,172.24.166.153,10.146.124.73,52506,22
2014-11-10 02:00:15,LOGIN FAILED,AUTH,user3,192.168.123.55,10.146.32.99,55750,22
How can I do that?
I know this works: perl -pe 's/(\d+)/localtime($1)/e'
(from this question) but the output format is Mon Nov 10 02:00:03 2014
.
I know this command can convert timestamps into my desired output: date -d@1415602803 +"%F %H:%M:%S"
, but I couldn't make it work with awk
using system("cmd")
because of all the quotations and whatnot.
awk 'BEGIN{ print strftime("%Y-%m-%d %H:%M:%S", 0) }'
givesawk: line 2: function strftime never defined
here. Please clarify that NOT EVERY AWK hasstrftime()
! For a solution running in most (all?) AWKs, please use the externaldate
command to stay portable. – Nov 25 '14 at 05:37date
implementations that can convert dates between formats as an extension, but how it's done varies completely between the implementations. For instance, the solution given by Costas uses a syntax specific to GNUdate
. If you want portability, useperl
. – Stéphane Chazelas Aug 17 '16 at 17:22