Need Help on date subtraction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help on date subtraction
# 1  
Old 11-16-2012
Need Help on date subtraction

I have dates as follows in a file

Code:
20121029135649
20121029135721
20121030091540
20121030093420
20121030094340
20121030095427
20121030095856
20121030100104
20121030100251

All these dates are in sorted order. I need to find out the difference between the dates as follows

2nd row date - 1st row date
3rd row date - 2nd row date
4th row date - 3rd row date

For eg:

Code:
20121029135649  20121029135721     difference in minutes
20121029135721  20121030091540     difference in minutes
20121030091540  20121030093420     difference in minutes
20121030093420  20121030094340     difference in minutes

I have tried using date -d "" +%s, but didn't find a correct solution.
I am using HP-UX OS. Please give me a solution.

Last edited by Scrutinizer; 11-16-2012 at 04:02 AM..
# 2  
Old 11-16-2012
You may have a go with this (although I feel someone will come up with a better solution soon):
Code:
perl -MTime::Local -lne 'if($. > 1) {
/(....)(..)(..)(..)(..)(..)/;
$currtime=timelocal($6,$5,$4,$3,($2-1),$1-1900);
printf "%s\t%s --> %.2f minutes\n",$prevline,$_,(($currtime-$prevtime)/60);
$prevline=$_;
$prevtime=$currtime;
next
}
$prevline=$_;
/(....)(..)(..)(..)(..)(..)/;
$prevtime=timelocal($6,$5,$4,$3,($2-1),$1-1900);
' file


Last edited by elixir_sinari; 11-16-2012 at 05:13 AM.. Reason: Thanks to itkamaraj!
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 11-16-2012
If you have the perl with Date::Calc module, then you can use Mktime to calculate the epoch. Then subtract the both epoch and divide it by 60 (seconds) to find the minutes.

Code:
$time = Mktime($year,$month,$day, $hour,$min,$sec);
 
you can retrieve the $year,$month....$sec by using the below
 
/(....)(..)(..)(..)(..)(..)/

This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 11-16-2012
Shell function and printf (ksh) examples.
# 5  
Old 11-17-2012
Code:
$ perl -M"Date::Calc 'Mktime'" -lane 'sub convert{$_[0]=~/(....)(..)(..)(..)(..)(..)/; return Mktime($1,$2,$3,$4,$5,$6)}if($.==1){$a=convert $F[0];$first=$_;next}$b=convert $F[0]; print $first." ".$_." ".($b-$a)/60;$first=$_;$a=$b' input.txt
20121029135649 20121029135721 0.533333333333333
20121029135721 20121030091540 1158.31666666667
20121030091540 20121030093420 18.6666666666667
20121030093420 20121030094340 9.33333333333333
20121030094340 20121030095427 10.7833333333333
20121030095427 20121030095856 4.48333333333333
20121030095856 20121030100104 2.13333333333333
20121030100104 20121030100251 1.78333333333333

# 6  
Old 11-17-2012
If you have ksh93:
Code:
#!/bin/ksh

LASTLINE=""
last=""

while read CURRLINE
do
    this=${CURRLINE:0:4}"-"${CURRLINE:4:2}"-"${CURRLINE:6:2}" "${CURRLINE:8:2}":"${CURRLINE:10:2}":"${CURRLINE:12:2}
    [[ -n $last ]] && {
        SEC=$(( $(printf "%(%s)T" "$this") - $(printf "%(%s)T" "$last") ))
        MIN=$(( SEC/60 ))
        SEC=$(( SEC%60 ))
        printf "%s  %s  %d:%02d\n" $CURRLINE $LASTLINE $MIN $SEC
    }
    LASTLINE=$CURRLINE
    last=$this
done < infile

Output for your examples
Code:
20121029135721 20121029135649 0:32
20121030091540 20121029135721 1158:19
20121030093420 20121030091540 18:40
20121030094340 20121030093420 9:20
20121030095427 20121030094340 10:47
20121030095856 20121030095427 4:29
20121030100104 20121030095856 2:08
20121030100251 20121030100104 1:47

# 7  
Old 11-18-2012
or Gnu sed/awk
Code:
[root@node3 ~]# cat get_date 
sed -r 's!(....)(..)(..)(..)(..)(..)!echo -n "& ";date -d "\1-\2\-\3 \4:\5:\6" +%s!eg' ${1} | \
awk 'NR!=1{printf("%s %s %02d:%02d:%02d", p,$1,($2-q)/3600,($2-q)%3600/60,($2-q)%3600%60);printf("\n")}{p=$1;q=$2}'
[root@node3 ~]# bash get_date infile 
20121029135649 20121029135721 00:00:32
20121029135721 20121030091540 19:18:19
20121030091540 20121030093420 00:18:40
20121030093420 20121030094340 00:09:20
20121030094340 20121030095427 00:10:47
20121030095427 20121030095856 00:04:29
20121030095856 20121030100104 00:02:08
20121030100104 20121030100251 00:01:47

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Date Subtraction

Hello All, I am a newbie to unix shell scripting and need to write a script that displays the difference between two variables that stores date value. For example, F1=`ls -ltr file1* | tail -1 |tr -s ' ' |cut -d' ' -f6,7,8` F2=`ls -ltr file2* | tail -1 |tr -s ' ' |cut -d' ' -f6,7,8` F1... (3 Replies)
Discussion started by: priyaa2010
3 Replies

2. Shell Programming and Scripting

Subtraction using arrays

Hello all . I have two arrays. ${ARRAY_MOUNT_POINT_CAPACITY} ${ARRAY_MOUNT_POINT_CAPACITY}. Whats the synatx of subtracting their values , placing them in variable V1 and then echoeing it ??? Ive tried expr and let ...gives me ./test_code.sh: difference: bad number (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. Shell Programming and Scripting

Subtraction

Hi #!/bin/sh month=`date +%m` year=`date +%Y` echo $month a=02 # Retaining Data for Current and Previous Month lmonth=`expr $month - $a` if test "$lmonth" = "0" then lmonth=12 year=`expr $year - 1` fi echo $year echo $lmonth The output is (3 Replies)
Discussion started by: Abhayman
3 Replies

4. Shell Programming and Scripting

Date Subtraction with time.

HI gurus... I have a PERL file that help me extract the date and time of the file. The format of this is: yyyymmddhhmmss. Example: 20100430070935 (April 30 2010 07:09:35) How can i subtract the acquired time from system's time..?? The answer... (6 Replies)
Discussion started by: bankimmehta
6 Replies

5. Shell Programming and Scripting

use of uninitialized value in subtraction

Hallo all i am trying to execute this script ............... But this is throwing the error...... use of uninitialized value in subtraction in at icd_convert.pl line 156 use of uninitialized value in subtraction in at icd_convert.pl line 157 use of uninitialized value in subtraction in at... (1 Reply)
Discussion started by: suvenduperl
1 Replies

6. Shell Programming and Scripting

Date subtraction

hi, i set up a script on my server to do a particular task once files from an external system are ftpd in the format compaq_20100110 (YYDDMM). Interestingly, the source of ftp is sending the files in the format e.g 20100109 i.e. previous date and for some reason this fails.kindly see my script... (2 Replies)
Discussion started by: bigtejus
2 Replies

7. Shell Programming and Scripting

awk subtraction

hi i have file 1 as follows: 6 7 8 9 10 i have file 2 as follows: 5 5 5 5 5 i want file 3 as follows: (4 Replies)
Discussion started by: npatwardhan
4 Replies

8. Linux

date subtraction(URGENT)

Hi all, I need the date subtraction fuctionality using shell commands. For example: date1:Wed Apr 5 08:35:21 IST 2006 date2:Tue Apr 4 10:35:44 IST 2006 I need the date subtraction result like " 22 hours 23 seconds". Please guide me to complete this task. Can you please help me ASAP. ... (3 Replies)
Discussion started by: uday123
3 Replies

9. UNIX for Dummies Questions & Answers

subtraction from date

hi gurus! i realize that my question shows my stupidness, but i need your help! i have: s_date=`date +%m-%d-%Y_%I%p` variable and i need the same, but minus one hour.. what i made: s_date=time(`date +%m-%d-%Y_%I%p`) - 3600 but i'm getting: daily_exports.sh: line 20: syntax error near... (4 Replies)
Discussion started by: MarGur
4 Replies

10. Shell Programming and Scripting

Date Subtraction in KSH

I need to figure out the numeric representation of the previous month (in an automated monthly-running script) so that I may append it to a filename. I have tried statements such as variable=`date +%m -1` (and several variations) but with no success. I have also tried simply assigning the value... (3 Replies)
Discussion started by: mharley
3 Replies
Login or Register to Ask a Question