The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Convert Epoch Time to Standard Date and Time & Vice Versa DrivesMeCrazy Shell Programming and Scripting 5 02-07-2009 01:40 AM
To convert multi format file to a readable ascii format gaur.deepti UNIX for Dummies Questions & Answers 5 03-25-2008 03:03 PM
convert mmddyy date format to ccyyddd format?? Bhups Shell Programming and Scripting 2 09-27-2006 11:30 PM
how to convert epoch time to readible format? cin2000 Shell Programming and Scripting 11 12-19-2005 07:14 PM
format date/time presentation apalex Shell Programming and Scripting 6 04-19-2002 01:34 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-18-2008
rk4k rk4k is offline
Registered User
  
 

Join Date: Nov 2008
Location: /planet-earth/indonesia/jakarta/
Posts: 11
Talking 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 (permalink)  
Old 11-19-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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 (permalink)  
Old 11-19-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,861
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 (permalink)  
Old 11-19-2008
rk4k rk4k is offline
Registered User
  
 

Join Date: Nov 2008
Location: /planet-earth/indonesia/jakarta/
Posts: 11
Thanks for both answer.

The gawk solution is cool
Sponsored Links
Closed Thread

Bookmarks

Tags
awk, sed, substitute

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:47 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0