LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Unix Time conversion (https://www.linuxquestions.org/questions/linux-newbie-8/unix-time-conversion-604263/)

dnoy 12-03-2007 07:50 PM

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,

gilead 12-03-2007 09:57 PM

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


dnoy 12-04-2007 08:52 AM

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,

chrism01 12-04-2007 05:23 PM

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

dnoy 12-04-2007 06:56 PM

what if i have thousands of lines, should i do some sort of a loop and stop at the end with what you suggested?

chrism01 12-04-2007 07:33 PM

Put that lot into a function and loop through your file, calling that fn for each record.

dnoy 12-04-2007 07:55 PM

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

chrism01 12-04-2007 10:58 PM

Here ya go: http://www.tldp.org/LDP/abs/html/
:)

Also, bookmark this: http://rute.2038bug.com/index.html.gz

dnoy 12-08-2007 09:59 PM

Thank you so much!

gilead 02-03-2008 10:36 PM

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 06:17 AM.