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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-03-2007, 07:50 PM
|
#1
|
Member
Registered: Nov 2007
Posts: 69
Rep:
|
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,
|
|
|
12-03-2007, 09:57 PM
|
#2
|
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141
Rep:
|
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.
|
|
|
12-04-2007, 08:52 AM
|
#3
|
Member
Registered: Nov 2007
Posts: 69
Original Poster
Rep:
|
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,
|
|
|
12-04-2007, 05:23 PM
|
#4
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398
|
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
|
|
|
12-04-2007, 06:56 PM
|
#5
|
Member
Registered: Nov 2007
Posts: 69
Original Poster
Rep:
|
what if i have thousands of lines, should i do some sort of a loop and stop at the end with what you suggested?
|
|
|
12-04-2007, 07:33 PM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398
|
Put that lot into a function and loop through your file, calling that fn for each record.
|
|
|
12-04-2007, 07:55 PM
|
#7
|
Member
Registered: Nov 2007
Posts: 69
Original Poster
Rep:
|
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
|
|
|
12-04-2007, 10:58 PM
|
#8
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398
|
|
|
|
12-08-2007, 09:59 PM
|
#9
|
Member
Registered: Nov 2007
Posts: 69
Original Poster
Rep:
|
Thank you so much!
|
|
|
02-03-2008, 10:36 PM
|
#10
|
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 09:59 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|