Visit The New, Modern Unix Linux Community


Convert Unix Time to Standard Time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Unix Time to Standard Time
# 1  
Convert Unix Time to Standard Time

I have a list of interfaces and time the interface was last active. I can't figure out how to convert the time in the second column,

PHP Code:
Fa1/14 0
Se0
/0/0 0
Fa1
/11 0
Fa1
/9 0
Fa1
/0 0
Se0
/0/1 1240401408
Gi1
/0 0
Fa0
/0 1240401408
Fa1
/3 0
Fa1
/8 0
Fa1
/15 0
Fa1
/13 0
Fa1
/10 0
Fa1
/1 0
Fa1
/12 1240401408
Fa0
/1 1240401408
Fa1
/5 1240401408
Fa1
/6 0
Se0
/0/0.1 0
Fa1
/7 0
Fa1
/2 0
Lo0 0
Fa1
/4 1240401408 
Can this be done with shell?
# 2  
many ways to skin that cat.
if you're Solaris:
Code:
#!/bin/ksh

file='interfaces.txt'

while read inter time
do
   printf "%s %s\n" "${inter}" "$(echo "0t${time}=Y" | /usr/bin/adb)"
done < "${file}"

# 3  
Hammer & Screwdriver This works, although not sure what you want to do with the 0 (ZERO) values

The zero values give an unusual (although technically correct) answer.

Code:
> cat proc13.sh
#/usr/bin/bash

while read param etime
   do
   echo $param $(date '+%Y-%m-%d' -d @$etime)
done <file13

> proc13.sh
Fa1/14 1969-12-31
Se0/0/0 1969-12-31
Fa1/11 1969-12-31
Fa1/9 1969-12-31
Fa1/0 1969-12-31
Se0/0/1 2009-04-22
Gi1/0 1969-12-31
Fa0/0 2009-04-22
Fa1/3 1969-12-31
Fa1/8 1969-12-31
Fa1/15 1969-12-31
Fa1/13 1969-12-31
Fa1/10 1969-12-31
Fa1/1 1969-12-31
Fa1/12 2009-04-22
Fa0/1 2009-04-22
Fa1/5 2009-04-22
Fa1/6 1969-12-31
Se0/0/0.1 1969-12-31
Fa1/7 1969-12-31
Fa1/2 1969-12-31
Lo0 1969-12-31
Fa1/4 2009-04-22

# 4  
Thanks for the replys.

I went about this a different way. But now I have a different issue.

Here is my code.

Code:
#!/bin/bash
file='file.txt'
while read inter time
do
   printf "%s %s\n" "${inter}" "perl -e 'print scalar localtime("${time}") . "\n";"
done < "${file}"

This is the result.

Code:
Fa1/14 perl -e 'print scalar localtime(0) . n;
Se0/0/0 perl -e 'print scalar localtime(0) . n;
Fa1/11 perl -e 'print scalar localtime(0) . n;
Fa1/9 perl -e 'print scalar localtime(0) . n;
Fa1/0 perl -e 'print scalar localtime(0) . n;
Se0/0/1 perl -e 'print scalar localtime(1240421065) . n;
Gi1/0 perl -e 'print scalar localtime(0) . n;
Fa0/0 perl -e 'print scalar localtime(1240421065) . n;
Fa1/3 perl -e 'print scalar localtime(0) . n;
Fa1/8 perl -e 'print scalar localtime(0) . n;
Fa1/15 perl -e 'print scalar localtime(0) . n;
Fa1/13 perl -e 'print scalar localtime(0) . n;
Fa1/10 perl -e 'print scalar localtime(0) . n;
Fa1/1 perl -e 'print scalar localtime(0) . n;
Fa1/12 perl -e 'print scalar localtime(1240421065) . n;
Fa0/1 perl -e 'print scalar localtime(1240421065) . n;
Fa1/5 perl -e 'print scalar localtime(0) . n;
Fa1/6 perl -e 'print scalar localtime(0) . n;
Se0/0/0.1 perl -e 'print scalar localtime(0) . n;
Fa1/7 perl -e 'print scalar localtime(0) . n;
Fa1/2 perl -e 'print scalar localtime(0) . n;
Lo0 perl -e 'print scalar localtime(0) . n;
Fa1/4 perl -e 'print scalar localtime(1240421065) . n;

I tried running perl -x script.sh but that does not work.
# 5  
Code:
#!/bin/bash
file='file.txt'
while read inter time
do
   printf "%s %s\n" "${inter}" "$(perl -e 'print scalar localtime("${time}") . "\n";')"
done < "${file}

# 6  
Quote:
Originally Posted by vgersh99
Code:
#!/bin/bash
file='file.txt'
while read inter time
do
   printf "%s %s\n" "${inter}" "$(perl -e 'print scalar localtime("${time}") . "\n";')"
done < "${file}


Hmm, different issue now? The $time variable is not being passed for some reason.

Code:
Fa1/14 Wed Dec 31 20:00:00 1969
Fa1/11 Wed Dec 31 20:00:00 1969
Se0/0/0 Wed Dec 31 20:00:00 1969
Fa1/9 Wed Dec 31 20:00:00 1969
Fa1/0 Wed Dec 31 20:00:00 1969
Fa1/3 Wed Dec 31 20:00:00 1969
Fa0/0 Wed Dec 31 20:00:00 1969
Gi1/0 Wed Dec 31 20:00:00 1969
Se0/0/1 Wed Dec 31 20:00:00 1969
Fa1/8 Wed Dec 31 20:00:00 1969
Fa1/15 Wed Dec 31 20:00:00 1969
Fa1/13 Wed Dec 31 20:00:00 1969
Fa1/10 Wed Dec 31 20:00:00 1969
Fa1/12 Wed Dec 31 20:00:00 1969
Fa1/1 Wed Dec 31 20:00:00 1969
Fa0/1 Wed Dec 31 20:00:00 1969
Fa1/6 Wed Dec 31 20:00:00 1969
Fa1/5 Wed Dec 31 20:00:00 1969
Se0/0/0.1 Wed Dec 31 20:00:00 1969
Fa1/7 Wed Dec 31 20:00:00 1969
Fa1/2 Wed Dec 31 20:00:00 1969
Lo0 Wed Dec 31 20:00:00 1969
Fa1/4 Wed Dec 31 20:00:00 1969

# 7  
sorry - my bad:

Code:
#!/bin/bash
file='file.txt'
while read inter time
do
   printf "%s %s\n" "${inter}" "$(perl -e 'print scalar localtime('"${time}"') . "\n";')"
done < "${file}


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #543
Difficulty: Easy
A global variable can be accessed and referenced on every line of code.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert UTC time into current UNIX sever time zone

Hi guys thanks for the help for my previous posts.Now i have a requirement that i download a XMl file which has UTC time stamp.I need to convert UTC time into Unix server timezone. For ex if the time zone of unix server is CDT then i need to convert into CDT.whatever may be the system time... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

2. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

3. Shell Programming and Scripting

Help: transform unix time format to standard

Hello All, Do you have any idea, how can I transform the unix time format to standard: time_last_login=1268057983 :confused: I would like to use the transformation in a AIX shell script :( (6 Replies)
Discussion started by: kalaso
6 Replies

4. Shell Programming and Scripting

Find and Convert UTC Time to PST Time

Hello All - I have a script that grabs data from the net and outputs the following data 46029 46.144 -124.510 2010 07 26 22 50 320 4.0 6.0 2.2 9 6.8 311 1012.1 -0.9 13.3 13.5 13.3 - - 46041 47.353 -124.731 2010 07 26 22 50 250 2.0 3.0 1.6 8 6.4 - 1011.6 - ... (0 Replies)
Discussion started by: drexnefex
0 Replies

5. 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

6. Shell Programming and Scripting

How do you convert scientific time to standard

Hi All, I'm new to this forum, and appreciate any assistance with my issue. I have a shell script that logs into an oracle DB and runs a sqlplus query. Everything works great except for the time I get. I'm new to shell so bare with me. What would be the code and where do I place it? My... (18 Replies)
Discussion started by: cg_dude
18 Replies

7. Shell Programming and Scripting

Convert date to unix time

Hi, I need to convert a date in the format yyyy-mm-dd to unix seconds, shell script or perl would be ok since there is no hour/second, we can assume 12am every day thanks in advance funksen (4 Replies)
Discussion started by: funksen
4 Replies

8. 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

9. Shell Programming and Scripting

Convert milliseconds to standard time

hello, I have the uptime of the server showing as upTime=2427742050 How do I convert it to standard time. Thanks Chiru (1 Reply)
Discussion started by: chiru_h
1 Replies

10. Shell Programming and Scripting

Convert from standard epoch time from a shell script?

Is there an easy method to do an on the fly conversion of a standard epoch time (seconds from 1970) to more readable date format? Does Unix have anything built in to do this? (4 Replies)
Discussion started by: LordJezo
4 Replies

Featured Tech Videos