time conversion using nawk

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?

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.

How about using 'date'?

 
$ epoch=1302510354 ; echo $epoch | awk '{system("date -d @" $1 " +\"%Y%m%d %H:%M:%S\""); }'
20110410 22:25:54

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 :slight_smile:

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