bing
April 10, 2011, 9:11pm
1
hi,
i've got an input file that contains
12345678 AAA
12345679 BBB
12345680 CCC
where 1st column is epoch time while the second column are some string.
I'm using nawk to do regular expression on the 2nd column and i need to convert the epoch time to human readable time e.g. 'yyyymmdd hh24:mi:ss'.
i cant find any info on time converion in nawk .. any suggestions on how i can do it in nawk?
mirni
April 10, 2011, 9:38pm
2
miro@miro-ntb:~/Desktop$ date +%s | awk '{print strftime("%Y-%m-%d %H:%M:%S",$1)}'
2011-04-10 15:37:14
As far as I know, nawk does not support strftime(). It is only supported by gawk. If gawk is available to you, Mirni's answer will work for you.
mirni
April 11, 2011, 4:33am
4
How about using 'date'?
$ epoch=1302510354 ; echo $epoch | awk '{system("date -d @" $1 " +\"%Y%m%d %H:%M:%S\""); }'
20110410 22:25:54
bing
April 11, 2011, 6:18am
5
hi,
thanx for the prompt response.
$1 contains the epoch time, thus i would need to do conversion on $1 ..
also i've have to read multiple files and convert n output them to their respective files ..
am not sure abt using gawk .. i'll try
Perl
perl -nle 'use Time::Local;@dt=localtime((split)[0]);printf "%4d%02d%02d%s\n",$dt[5]+1900,$dt[4]+1,$dt[3]," $dt[2]:$dt[1]:$dt[0]";' inputfile