LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-06-2012, 12:58 PM   #1
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Rep: Reputation: 15
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!
 
Old 01-06-2012, 01:29 PM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,798

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
Hello,

It would have to be like follows:
Code:
awk '{$2=strftime("%c",$2)} {print $2}' myfile
Cheers,

Josh
 
1 members found this post helpful.
Old 01-06-2012, 03:20 PM   #3
corp769
Guru
 
Registered: Apr 2005
Posts: 5,798

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
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
 
Old 01-06-2012, 11:13 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
You can simplify as well if you wish to print the whole line:
Code:
awk '$2=strftime("%c",$2)' file
 
Old 01-07-2012, 02:39 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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).
 
1 members found this post helpful.
Old 01-07-2012, 05:18 AM   #6
corp769
Guru
 
Registered: Apr 2005
Posts: 5,798

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
Quote:
Originally Posted by colucix View Post
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!
 
Old 01-08-2012, 02:13 AM   #7
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
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
 
  


Reply

Tags
awk, date


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Human readable output from HP-UX Ejdaha Linux - Newbie 6 05-09-2013 02:05 AM
gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)? BrianK Programming 23 10-30-2010 04:20 AM
Human readable IP adresses from netfilter dividedby0 Linux - Networking 3 03-22-2009 12:10 PM
Java Epoch time to Human Readable (using Date) true_atlantis Programming 1 05-09-2008 05:13 PM
Webalizer - howto convert the trafic into human readable? sys7em Linux - Server 3 09-25-2006 04:48 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration