LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-03-2007, 07:50 PM   #1
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Rep: Reputation: 15
Unix Time conversion


I have logs that i am looking through and the first line is unix time

ie:

1196723947.101

the full log looks something like this

1196723947.101 987 192.168.1.1 text goes here

i want to take the unix time and convert it to regular time

20071203-23:19:07 GMT

can anyone help me achieve this?

Thank you,
 
Old 12-03-2007, 09:57 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
You can use the date command for that. Using the value you have in your log:
Code:
date --date=@1196723947.101 
Tue Dec  4 09:19:07 EST 2007
Here is the same with some formatting codes:
Code:
~$ date --date=@1196723947.101 +"%Y%m%d-%H:%M:%S %Z"
20071204-09:19:07 EST
$ date --date=@1196723947.101 +"%Y%m%d-%X %Z"
20071204-09:19:07 AM EST
$ date --date=@1196723947.101 +%c            
Tue 04 Dec 2007 09:19:07 AM EST

Last edited by gilead; 12-03-2007 at 10:45 PM.
 
Old 12-04-2007, 08:52 AM   #3
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Original Poster
Rep: Reputation: 15
gilead,

Thanks a lot for your reply. I want to do a little more then issue the command manually. While i grep the file i get

1196723947.101 987 192.168.1.1 text goes here

i would like to some how | the date command and get

Tue Dec 4 09:19:07 EST 2007 987 192.168.1.1 text goes here

or

20071204-09:19:07 AM EST 987 192.168.1.1 text goes here

Im just not sure how to take the unix time from the grep and send it to the date command and have it reformat my output

Thank you,
 
Old 12-04-2007, 05:23 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398

Rep: Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780
Store result from grep in a var and use cut cmd:

rec=`grep ....`
mydate=`echo $line|cut -d' ' -f1`
text=`echo $line|cut -d' ' -f2`

fmt_date=`date --date=@${mydate} +"%Y%m%d-%X %Z"`

echo $fmt_date $text
 
Old 12-04-2007, 06:56 PM   #5
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Original Poster
Rep: Reputation: 15
what if i have thousands of lines, should i do some sort of a loop and stop at the end with what you suggested?
 
Old 12-04-2007, 07:33 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398

Rep: Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780
Put that lot into a function and loop through your file, calling that fn for each record.
 
Old 12-04-2007, 07:55 PM   #7
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Original Poster
Rep: Reputation: 15
do you have any good web sites to point me to, as i am new to scripting?

I am not sure how to do functions in bash
 
Old 12-04-2007, 10:58 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398

Rep: Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780
Here ya go: http://www.tldp.org/LDP/abs/html/


Also, bookmark this: http://rute.2038bug.com/index.html.gz
 
Old 12-08-2007, 09:59 PM   #9
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Original Poster
Rep: Reputation: 15
Thank you so much!
 
Old 02-03-2008, 10:36 PM   #10
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
I don't know if you ever got this working, but I needed a one-liner to parse my Squid log today and the following did the job:
Code:
awk '{print strftime("%c",$1),$4,$7}' access.log
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Disabling auto conversion to daylight savings time Saxton Linux - General 3 02-12-2009 08:13 AM
How to find the time in unix ? vskgopu Linux - Newbie 6 05-16-2006 02:03 AM
24 to 12 hour time conversion command? true_atlantis Linux - Newbie 1 03-24-2006 07:52 PM
DOS -> UNIX file conversion issue inspleak Linux - Newbie 5 07-04-2004 12:24 AM
unix time chrismiceli Linux - General 1 01-07-2003 10:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:59 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration