Convert Epoch time format to normal date time format in the same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Epoch time format to normal date time format in the same file
# 1  
Old 11-18-2008
Computer Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these :

Code:
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '87.204.76.129', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 15485, 'time': 1226506312L, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '118.125.238.96', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 23786, 'time': 1226506312L, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '90.4.40.186', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 27542, 'time': 1226506312L, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '84.90.16.109', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 64115, 'time': 1226506312L, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '78.106.166.60', 'client_port': 45309, 'client': '10.64.68.143', 'server_port': 57587, 'time': 1226506312L, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '218.161.75.90', 'client_port': 45309, 'client': '10.64.68.143', 'server_port': 14323, 'time': 1226506312L, 'serverhostname': ''}
{'protocol': 6, 'service': 'BitTorrent encrypted transfer', 'server': '208.53.147.63', 'client_port': 3647, 'client': '10.64.68.68', 'server_port': 31866, 'time': 1226534386L, 'serverhostname': ''}
{'protocol': 6, 'service': 'BitTorrent transfer', 'server': '87.120.14.250', 'client_port': 41607, 'client': '10.64.68.78', 'server_port': 22566, 'time': 1226534379L, 'serverhostname': ''}
{'protocol': 6, 'service': 'HTTP', 'server': '72.14.221.190', 'client_port': 51980, 'client': '10.64.68.68', 'server_port': 80, 'time': 1226880065L, 'serverhostname': ''}
{'protocol': 6, 'service': 'Undetermined', 'server': '92.97.199.57', 'client_port': 55456, 'client': '10.64.68.68', 'server_port': 25252, 'time': 1226880097L, 'serverhostname': ''}
{'protocol': 6, 'service': 'HTTP', 'server': '74.125.19.118', 'client_port': 54043, 'client': '10.64.68.68', 'server_port': 80, 'time': 1226880099L, 'serverhostname': ''}

I want to convert the field that contains 1226506312L to normal date format. I have found the script to convert it with date command with :

Code:
date -d '1970-01-01 '$1' seconds'

I want to convert the file so it will look like :

Code:
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '84.90.16.109', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 64115, 'time': Wed Nov 12 16:11:52, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '78.106.166.60', 'client_port': 45309, 'client': '10.64.68.143', 'server_port': 57587, 'time': Wed Nov 12 16:11:52, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '218.161.75.90', 'client_port': 45309, 'client': '10.64.68.143', 'server_port': 14323, 'time': Wed Nov 12 16:11:52, 'serverhostname': ''}
{'protocol': 6, 'service': 'BitTorrent encrypted transfer', 'server': '208.53.147.63', 'client_port': 3647, 'client': '10.64.68.68', 'server_port': 31866, 'time': Wed Nov 12 23:59:46 WIT 2008, 'serverhostname': ''}
{'protocol': 6, 'service': 'BitTorrent transfer', 'server': '87.120.14.250', 'client_port': 41607, 'client': '10.64.68.78', 'server_port': 22566, 'time': Wed Nov 12 23:59:46 WIT 2008, 'serverhostname': ''}
{'protocol': 6, 'service': 'HTTP', 'server': '72.14.221.190', 'client_port': 51980, 'client': '10.64.68.68', 'server_port': 80, 'time': Wed Nov 12 23:59:46 WIT 2008, 'serverhostname': ''}
{'protocol': 6, 'service': 'Undetermined', 'server': '92.97.199.57', 'client_port': 55456, 'client': '10.64.68.68', 'server_port': 25252, 'time': Wed Nov 12 23:59:46 WIT 2008, 'serverhostname': ''}
{'protocol': 6, 'service': 'HTTP', 'server': '74.125.19.118', 'client_port': 54043, 'client': '10.64.68.68', 'server_port': 80, 'time': Wed Nov 12 23:59:46 WIT 2008, 'serverhostname': ''}

I know a little about awk, and can convert it with :

Code:
awk '{ print $7 }' FS="," suspected | awk '{ print $2 }' | cut -f1 -d"L" | while read line ; do sh datepoch $line ; done

but the script only just convert and output date field.

How to convert and also substitute the date format to the same file ?

Thanks for any help.

Regards
# 2  
Old 11-19-2008
awk's date and time capabilities are limited, so I usually use perl when manipulating those. Try this:

Code:
perl -pe ' if ($_ =~ /.time.: ([0-9]+)L/) { s/$1L/scalar localtime/e; }' inputfile > outputfile

The date format isn't quite what you were looking for, but you can do some other jiggery pokery with strftime or similar.
# 3  
Old 11-19-2008
Another way would be to use gawk i.e.
Code:
gawk 'BEGIN { FS=","; OFS="," } {$7=" \47time\47: "strftime("%a %b %d %T", substr($7,10,10)); print }' file

# 4  
Old 11-19-2008
Thanks for both answer.

The gawk solution is cool
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert epoch time stamp into human readable format

Can someone help me to write a shell script to convert epoch timestamp into human readable format 1394553600,"test","79799776.0","19073982.728571","77547576.0","18835699.285714" 1394553600,"test1","80156064.0","19191275.014286","62475360.000000","14200554.720000"... (10 Replies)
Discussion started by: Moon1234
10 Replies

2. Shell Programming and Scripting

Convert epoch time to Julian date

Need assistance in converting an epoch time to Julian date To get epoch perl -e 'use Time::Local; print timelocal(1,5,2,12,10,2008), "\n"' (3 Replies)
Discussion started by: ajayram_arya
3 Replies

3. Shell Programming and Scripting

converting epoch time to ddmmyy format

I can not find a working script or way to do this on sun solaris , can someone please guide me? e.g 1327329935 epoch secs = 012312 (ddmmyy) thanks (5 Replies)
Discussion started by: aliyesami
5 Replies

4. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 Replies

5. Shell Programming and Scripting

Convert epoch to human readable date & time format

Hello I have log file from solaris system which has date field converted by Java application using System.currentTimeMillis() function, example is 1280943608380 which equivalent to GMT: Wed, 04 Aug 2010 17:40:08 GMT. Now I need a function in shell script which will convert 1280943608380... (3 Replies)
Discussion started by: Yaminib
3 Replies

6. Shell Programming and Scripting

Need to convert an epoch date to MMDDYYHHmm format

System: HP-UX Kornshell Perl is installed, but not POSIX Hello, I am calculating a future date/time. To do this I take the system date in epoch format and add to it. I now need to take the new epoch date and convert it to MMDDYYHHmm format. Any help with this is greatly appreciated. (4 Replies)
Discussion started by: LetsGoPens
4 Replies

7. UNIX for Dummies Questions & Answers

getting the current time in Epoch format

Hi everybody, I want to get the current time in epoch format (in UNIX or Korn Shell) and store it in a variable called currentTime. Any response will be highly appreciated:) Thanks in advance, omoyne:D (8 Replies)
Discussion started by: omoyne
8 Replies

8. Shell Programming and Scripting

how to convert date time to epoch time in solaris

Hi, Is there any easy way to convert date time(stored in shell variable ) to epoch time in solaris box? As +%s is working on linux but not on solaris, also -d option is not working. Any suggestion please? (6 Replies)
Discussion started by: anshuman0507
6 Replies

9. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies

10. Shell Programming and Scripting

how to convert epoch time to readible format?

Hi, I would like to convert epoch time from the logs to readible fromat. How do I do it within shell? Thanks! (11 Replies)
Discussion started by: cin2000
11 Replies
Login or Register to Ask a Question