LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Convert field from epoch to human readable (http://www.linuxquestions.org/questions/programming-9/convert-field-from-epoch-to-human-readable-922445/)

sebelk 01-06-2012 12:58 PM

Convert field from epoch to human readable
 
Hi,

I have a file as follows:

Code:

10.129.187.1    1325914841 98-4B-4A-E4-AA-0E 10.129.89.194 * *
10.129.187.1    1325900260 2C-74-37-E8-D9-99 10.129.89.11 BLACKBERRY-54C6 01-3C-74-37-D8-D9-99
10.129.90.1    1325916914 4C-74-37-72-B7-09 10.129.90.76 BLACKBERRY-821D 01-3C-74-37-52-B7-09
10.129.90.1    1325906004 E6-3E-B6-26-C3-28 10.129.90.151 BLACKBERRY-CA1B 01-E8-3E-B6-23-C3-28

I'd want to convert 2nd field that is epoch time to an human readable format, I've tried with:

Code:

awk 'BEGIN {$2=strftime("%c",$2)} {print}' myfile
But it outputs nothing

Please could you help to do it?

Thanks in advance!

corp769 01-06-2012 01:29 PM

Hello,

It would have to be like follows:
Code:

awk '{$2=strftime("%c",$2)} {print $2}' myfile
Cheers,

Josh

corp769 01-06-2012 03:20 PM

Noticed you gave me rep; Did that help and/or solve your problem? If so, could you kindly mark your thread as solved using the thread tools located at the top of the page? Thanks!

Cheers,

Josh

grail 01-06-2012 11:13 PM

You can simplify as well if you wish to print the whole line:
Code:

awk '$2=strftime("%c",$2)' file

colucix 01-07-2012 02:39 AM

An aside note: awk here is useful to extract the 2nd field from a file, but in general, you can convert from epoch to calendar date using the GNU date command, example:
Code:

$ date -ud @1325914841
Sat Jan  7 05:40:41 UTC 2012

Note the @ before the number: it informs the date command that the argument is a time in seconds from epoch (otherwise it tries to interpret it as a date of some - unknown - type).

corp769 01-07-2012 05:18 AM

Quote:

Originally Posted by colucix (Post 4568593)
An aside note: awk here is useful to extract the 2nd field from a file, but in general, you can convert from epoch to calendar date using the GNU date command, example:
Code:

$ date -ud @1325914841
Sat Jan  7 05:40:41 UTC 2012

Note the @ before the number: it informs the date command that the argument is a time in seconds from epoch (otherwise it tries to interpret it as a date of some - unknown - type).

Good to know that..... Thanks!

sebelk 01-08-2012 02:13 AM

Quote:

Originally Posted by colucix (Post 4568593)
An aside note: awk here is useful to extract the 2nd field from a file, but in general, you can convert from epoch to calendar date using the GNU date command, example:
Code:

$ date -ud @1325914841
Sat Jan  7 05:40:41 UTC 2012

Note the @ before the number: it informs the date command that the argument is a time in seconds from epoch (otherwise it tries to interpret it as a date of some - unknown - type).

I knew it but I couldn't use in this case, I've found awk is better here... thanks anyway


All times are GMT -5. The time now is 11:23 PM.