Add/Subtract Time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add/Subtract Time
# 1  
Old 04-30-2010
MySQL Add/Subtract Time

need some help on the below requirement:

Code:
File1:
SV,22,20100501140000,JFK,RUH
SV,29,20100501073000,BOM,RUH
SV,29,20100501073000,SIN,RUH

third filed is datetime which is of the format (yyyymmddhh24miss)

Code:
File2
JFK,+,0500
BLR,-,0530
SIN,-,0800

for every line of file 1, take 4 field 'JFK' lookup on File2 first field ==JFK take the operation from the 2nd field and value of OFF-SET value on 3rd field which are "+" and "0500" respectively

add/subtract this lookup values from the date time filed of file1

The final display now should be
Code:
SV,22,20100501190000,JFK,RUH
SV,29,20100501020000,BOM,RUH
SV,29,20100430233000,SIN,RUH

Regards
Sampy

Last edited by ssantoshss; 04-30-2010 at 10:52 AM.. Reason: adding code tag
# 2  
Old 04-30-2010
Take a look at Yesterdays Date/Date Arithmetic - The UNIX and Linux Forums
For anything else please state your operating system.
# 3  
Old 04-30-2010
This needs to be run on Solaris 8
# 4  
Old 04-30-2010
Here's one way to do it with Perl -

Code:
$ 
$ 
$ cat -n file1
     1    SV,22,20100501140000,JFK,RUH
     2    SV,29,20100501073000,BOM,RUH
     3    SV,29,20100501073000,SIN,RUH
$ 
$ cat -n file2
     1    JFK,+,0500
     2    BLR,-,0530
     3    SIN,-,0800
$ 
$ 
$ ##
$ perl -F, -M"Date::Calc qw(Add_Delta_DHMS)" -lane '
>   if ($ARGV eq "file2") {
>     $os{$F[0]} = $F[1].join($F[1], unpack("A2A2",$F[2]));
>   } else {
>     @x = unpack("A4A2A2A2A2A2",$F[2]);
>     if (defined $os{$F[3]}) {
>       @x = Add_Delta_DHMS(@x,0,substr($os{$F[3]},0,3),substr($os{$F[3]},3),0);
>     }
>     printf("%s,%s,%04d%02d%02d%02d%02d%02d,%s,%s\n",$F[0],$F[1],@x,$F[3],$F[4]);
>   }
> ' file2 file1
SV,22,20100501190000,JFK,RUH
SV,29,20100501073000,BOM,RUH
SV,29,20100430233000,SIN,RUH
$ 
$

tyler_durden
# 5  
Old 05-01-2010
I dont know how to use perl....but when i try and execute this on Solaris 8...below is the error message

Can't locate Date/Calc.pm in @INC (@INC contains: /usr/perl5/5.8.4/lib/sun4-solaris-64int /usr/perl5/5.8.4/lib /usr/perl5/site_perl/5.8.4/sun4-solaris-64int /usr/perl5/site_perl/5.8.4 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.8.4/sun4-solaris-64int /usr/perl5/vendor_perl/5.8.4 /usr/perl5/vendor_perl .).
BEGIN failed--compilation aborted.
# 6  
Old 05-01-2010
Quote:
Originally Posted by ssantoshss
I dont know how to use perl....
You'll obviously have to learn it if you intend to implement the Perl solution. You'll find lots of good books on Perl available at your favorite bookstore.

Quote:
...but when i try and execute this on Solaris 8...below is the error message

Can't locate Date/Calc.pm in @INC (@INC contains: /usr/perl5/5.8.4/lib/sun4-solaris-64int /usr/perl5/5.8.4/lib /usr/perl5/site_perl/5.8.4/sun4-solaris-64int /usr/perl5/site_perl/5.8.4 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.8.4/sun4-solaris-64int /usr/perl5/vendor_perl/5.8.4 /usr/perl5/vendor_perl .).
BEGIN failed--compilation aborted.
This means that the "Date::Calc" module is not installed in your system. Check the CPAN website (cpan.org) that has this module as well as installation instructions in its FAQ section.

tyler_durden
# 7  
Old 05-01-2010
Dear Tyler...I dont have any access to install / remove softwares on these servers...I just have an account on this machine.
Thanks a lot for your help...

Any other solutions that can help me out on this problem.

Last edited by ssantoshss; 05-01-2010 at 02:02 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subtract time in two line

INPUT: 16:45:51 10051 77845 16:45:51 10051 77845 16:46:52 10051 77846 16:46:53 10051 77846 Match the last PID then subtract second line time with first line. Please help me with any command or script. (3 Replies)
Discussion started by: vivekn
3 Replies

2. UNIX for Dummies Questions & Answers

How to chnage format and subtract time and date and get average.?

Hello All , Please support for below request how to change format and subtract time and date and get average. xxx 13-OCT-15 11.32.18.241000 AM 13-OCT-15 11.35.49.089080 AM xxx 13-OCT-15 11.32.24.000000 AM 13-OCT-15 11.45.17.810904 AM xxx 13-OCT-15 11.32.25.232000 AM ... (1 Reply)
Discussion started by: mirwasim
1 Replies

3. Shell Programming and Scripting

Date command - subtract from given time

the given time is: 12:13:00 how do i subtract a 10 minutes from any given time? date '12:13:00' '-10 min' also tried this: date +12:13:00 '-10 min' (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Shell Programming and Scripting

Add or Subtract the hours,minutes or seconds in the the time variable

Hello All, I am working on script where I need to add hours,minutes or seconds in the time.Time is not the current but it could be future time.I thought I can store that time in variable and add hours.minutes or second but I am not able to add that in the time that is stores in a variable. Time... (9 Replies)
Discussion started by: anuragpgtgerman
9 Replies

5. Shell Programming and Scripting

Script to subtract time stored in a variable

Hello, I am writing a script to find time difference between two timestamp stored in a variable. i have two variable t1=11:48:30 t2=13:13:48 how i can find the difference i.e t2-t1 in seconds. Please help (4 Replies)
Discussion started by: anand2308
4 Replies

6. Shell Programming and Scripting

Subtract two rows (in Time format)

Hello all, I have written sth like this: #!/bin/bash grep -e XXX -e YYYY myfile.log | grep -v ZZZ | awk '{print $1 " " $2 ";" $3 ";" $9 ";" $11}' > myfile.csv sed -i '1iDate;Time;From;To' myfile.csv => it is clear that it converts log to csv and add a header. Now I want to subtract row... (4 Replies)
Discussion started by: frhling
4 Replies

7. Shell Programming and Scripting

Find the script running time and subtract from sleeptime

HI Guys, I want to find out the script running time and subtract from sleeptime. My Script Below Give me error :- #!/usr/bin/ksh timeout=100 start=$SECONDS sleep 20 end=$SECONDS echo "Time: $((end - start)) " ScTime = $((end - start)) (1 Reply)
Discussion started by: asavaliya
1 Replies

8. Shell Programming and Scripting

How to subtract time by 10 minutes in datecalc tool

Hi guys. I am trying to subtract 10 minutes from the current Unix system date and time. I have the datecalc provided here but it is mainly the date and not the time. Please check on how can i subtract 10 minutes from the current time using datecalc or any other shell scripting that will... (2 Replies)
Discussion started by: bantiloe
2 Replies

9. Shell Programming and Scripting

subtract minutes from time

i have the time 20100421043335 in format (date +%Y%m%d%H%M%S),and i want to be able to get the previous time 2 minutes ago,which is 20100421043135 (9 Replies)
Discussion started by: tomjones
9 Replies

10. Shell Programming and Scripting

Subtract Time

Hello, Im writing a script using the ksh shell. I have 2 variables in the script: CURRTIME PREVTIME Example, if CURRTIME=13:00, I want to somehow calculate what the time was an hour ago so that PREVTIME=12:00 Right now I have the following: CURRTIME=`date +%H:%M` How can I... (4 Replies)
Discussion started by: xadamz23
4 Replies
Login or Register to Ask a Question