epoch conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting epoch conversion
# 1  
Old 09-01-2009
epoch conversion

I need to convert an epoch time from a file into a standard UTC time and output it in the same format but I'm not sure what's the best approach

here's the input file and the bold part is what I need to convert.

1,1,"sys1",60,300000
2,"E:",286511144960
3,1251194521,"E:",0,0
3,1251194581,"E:",8,0
3,1251194641,"E:",17,0
3,1251194701,"E:",8,0
3,1251194761,"E:",25,0
3,1251194821,"E:",34,0
3,1251194881,"E:",145,0
3,1251194941,"E:",1646,0
3,1251195001,"E:",8,0

I want the output file to look like this

1,1,"sys1",60,300000
2,"E:",286511144960
3,Tue Aug 25 11:02:01 2009,"E:",0,0
3,Tue Aug 25 11:03:01 2009,"E:",8,0
3,Tue Aug 25 11:04:01 2009,"E:",17,0
3,Tue Aug 25 11:05:01 2009,"E:",8,0
3,Tue Aug 25 11:06:01 2009,"E:",25,0
3,Tue Aug 25 11:07:01 2009,"E:",34,0
3,Tue Aug 25 11:08:01 2009,"E:",145,0
3,Tue Aug 25 11:09:01 2009,"E:",1646,0
3,Tue Aug 25 11:10:01 2009,"E:",8,0

So I googled around and wrote some lines but I don't think it's the way to do it.

##############################################################
#!/bin/ksh


#skip 1st 2 lines and grab the epoch time
tail +3 file1| awk -F',' '{print $2}' > /temp/time

## replace the epoch time with UTC
for time in `cat /temp/time| awk '{print $1}'`
do
perl -e 'print scalar localtime ('$time'), "\n"'

done

rm /temp/time
##############################################################


I now have the time converted but how do I output it in the same format ?
I don't script too often as you can see and would appreciate assistance.

Cheers.
G
# 2  
Old 09-01-2009
Try something like this :

Code:
head -2 inputfile.txt > op.txt

for i in `tail +3 rem.txt`
do
time=`echo $i | awk -F, '{ print $2}'`
con=`perl -e 'print scalar localtime ('$time')'`
grep "$time" inputfile.txt | sed s/"$time"/"$con"/ >> op.txt
done

# 3  
Old 09-01-2009
Bug

Quote:
Originally Posted by panyam
Try something like this :

Code:
head -2 inputfile.txt > op.txt

for i in `tail +3 inputfile.txt`
do
time=`echo $i | awk -F, '{ print $2}'`
con=`perl -e 'print scalar localtime ('$time')'`
grep "$time" inputfile.txt | sed s/"$time"/"$con"/ >> op.txt
done

Brilliant. You're a genius. I get it. Gotta learn more and practice.
G
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python conversion to epoch time

Hi. I have timestamps that I am trying to convert to epoch time. An example: I am trying to convert this to an epoch timestamp but have one little glitch. I have this: import time date_time = '' pattern = '' epoch = int(time.mktime(time.strptime(date_time, pattern))) print epoch... (1 Reply)
Discussion started by: treesloth
1 Replies

2. Shell Programming and Scripting

Different epoch conversion result for bash and csh users

Hi there I'm using this script to convert command line history with Epoch time stamp to human readable. While it works fine with users with /bin/csh shell, it fails to convert for users with /bin/bash shell. Why is this happening? I even changed and added * and after the # but it still didnt... (2 Replies)
Discussion started by: hedkandi
2 Replies

3. Shell Programming and Scripting

Epoch in Perl

Hi, Can anybody tell me how time is calculated in the below or what is actually being done here? Also can you explain in simple words about epoch time and why it is used? Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

4. Shell Programming and Scripting

Shell Script for Epoch Time Conversion

Hi there I came across this script online to convert Epoch time to proper date format, but I am receiving the following error Also, I have HISTTIMEFORMAT set in user's .profile so that their history output shows time stamps. Additionally I have changed their .history location to a dedicated... (9 Replies)
Discussion started by: hedkandi
9 Replies

5. Shell Programming and Scripting

Epoch & Unix Timestamp Conversion Tools

Hi All, Please read the below data carefully. I need an unix command for converting unix timestamp to Epoch timestamp. I need to daily convert this today's unix(UTC) time to epoch time, so i am thinking to make a shellscript for this. Please help me for this by providing... (3 Replies)
Discussion started by: aish11
3 Replies

6. Shell Programming and Scripting

Date conversion from Standard/given format to seconds/epoch

I am trying get time difference of two dates in secs. Initially I want to convert a standard date format to epoch for two dates and then subtract the two epoch dates. Example : date -d "2007-09-01 17:30:40" '+%s' But this gives me below error date: illegal option -- d Usage: date OS: AIX... (6 Replies)
Discussion started by: bpaac
6 Replies

7. Shell Programming and Scripting

Epoch time

Guys, i have a question... I have 2 sets of data say "a" and "a+1" which has values in epoch time.. Question is... if i were to get the time difference where diff = "a+1" - "a" can i convert it back to real time duration after the subtraction... OR i need to convert em first before i do the... (1 Reply)
Discussion started by: 12yearold
1 Replies

8. Shell Programming and Scripting

conversion from EPOCH timestamp to local time zone

hello gurus, i want a perl/shell script which once invoked should convert a set of EPOCH timestamps to local time ( IST..i want) . how does it work ,i have an idea on that..but writing a perl/shell script for it is not possible for me...so i need help for the same. my exact requirement is... (2 Replies)
Discussion started by: abhijeetkul
2 Replies

9. UNIX for Dummies Questions & Answers

Epoch

Hi all, i am trying to figure out how i can get a 'Nix box to display epoch time. Is there a command to do this? Do I know what I am talking about or am I an Idiot? Wait dont answer that last question!!!!! Thanx in advance!!!:confused: (2 Replies)
Discussion started by: Bodhi
2 Replies

10. Programming

Epoch problem

I would like to know if the "Epoch" problem (on September 9, 2001) i.e. when the Unix clock counter will hit 100000000 will create a problem for programs that are dependent on system and server times. I am presently part of a team that is working on Oracle database on SUN SOLARIS based servers.... (2 Replies)
Discussion started by: vsomanchi
2 Replies
Login or Register to Ask a Question