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, |
You can use the date command for that. Using the value you have in your log:
Code:
date --date=@1196723947.101 Code:
~$ date --date=@1196723947.101 +"%Y%m%d-%H:%M:%S %Z" |
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, |
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 |
what if i have thousands of lines, should i do some sort of a loop and stop at the end with what you suggested?
|
Put that lot into a function and loop through your file, calling that fn for each record.
|
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 |
Here ya go: http://www.tldp.org/LDP/abs/html/
:) Also, bookmark this: http://rute.2038bug.com/index.html.gz |
Thank you so much!
|
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 02:31 AM. |