Subtracting time with awk - BASH/Debian GNU Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtracting time with awk - BASH/Debian GNU Linux
# 1  
Old 12-20-2009
MySQL Subtracting time with awk - BASH/Debian GNU Linux

I'm sure this is simple and I've been looking at examples for days on end but can't seem to come to grips with awk. What I have:
Code:
mplayer -v dvd:// -identify -vo null -ao null -nolirc -nojoystick -frames 0 2>/dev/null >> /tmp/MplayerOut
ChapterStart=($(grep CHAPTERS: /tmp/MplayerOut |sed 's/CHAPTERS://; s/,/\n /g'))

Output is like so:
Code:
 00:00:00
 00:01:03
 00:05:57
 00:08:27
 00:11:58
 00:14:50
 00:20:19
 00:25:06
 00:33:17
 00:38:21
 00:42:30
 00:46:11
 00:51:33
 01:00:04
 01:00:56
 01:04:15
 01:09:13
 01:16:51
 01:20:03
 01:27:58

What I'm trying to achieve is the difference between chapter 1 start time and chapter 2 start time and so on subtracting each chapter start time from the next in the array e.g. 00:05:57 - 00:01:03 = 00:04:54

Thanks for any advice.
# 2  
Old 12-21-2009
check if this helps

Code:
#!/usr/bin/perl

my $initial = '';
while (<>){
        chomp;
        $initial = ($initial) ? get_diff($initial,$_) : $_;
}

sub get_diff {
        my ($d1,$d2) = @_;
        my($h1,$m1,$s1) = split(":",$d1);
        my($h2,$m2,$s2) = split(":",$d2);
        my $t1 = $s1+($m1*60)+($h1*60*60); #secs
        my $t2 = $s2+($m2*60)+($h2*60*60); #secs
        my $diff = $t2 - $t1;
        my $f_t = sprintf("%02d:%02d:%02d",int ($diff/(60*60)),($diff/60) % 60,$diff % 60);
        print "$d1\t$d2\t$f_t\n";
        return $f_t;
}

Code:
cat abc.txt | perl scriptname.pl

HTH,
PL
# 3  
Old 01-07-2010
Starting with ghostdog74's example (which of course works perfectly) I have thus far been unable to make this work to subtract chapter start/end times. Perhaps someone can spot the reason(s)?

Code:
#!/bin/bash
awk 'BEGIN{
{
 for(i=1;i<=NF;i++){
    m=split($i,t,":")
    n=split($(i+1),w,":")
    chap = (t[1]*3600) + (t[2]*60) + t[3]
    chap_next = (w[1]*3600) + (w[2]*60) + w[3]
    duration = (chap_next - chap)
    print $duration
    }
 }
}'ChapterStart

Code:
$ cat ChapterStart 
00:00:00 00:01:03 00:05:57 00:08:27 00:11:58 00:14:50 00:20:19 00:25:06 00:33:17 00:38:21 00:42:30 00:46:11 00:51:33 01:00:04 01:00:56 01:04:15 01:09:13 01:16:51 01:20:03 01:27:58

# 4  
Old 02-15-2010
Finally it is working:
ChapStart is an array with start times in this format: 00:00:00 which gets converted to "Seconds since 1970-01-01"with 'date'. This allows us to do the math. Then, we convert it back to 00:00:00 format.

Code:
#~ Time conversion and calculations
for ((s=0; s<=$CHAPTERS; s++)) ## Convert ChapStart 00:00:00 to ChapTimeStart seconds since 1970-01-01
	do ChapTimeStart[$s]=$(date +%s -d "${ChapStart[$s]}") 
done

for ((e=0; e<$CHAPTERS; e++)) ## Convert ChapEnd 00:00:00 to ChapTimeEnd seconds since 1970-01-01
	do ChapTimeEnd[$e]=$(date +%s -d "${ChapEnd[$e]}")
done
	unset ChapTimeEnd[0] ##Remove the first chap start time 00:00:00
	ChapTimeEnd[$CHAPTERS]=$Disc ## Add $Disc as last array element
	ChapTimeEnd=( "${ChapTimeEnd[@]}" ) ## Pack the array
	
	unset ChapEnd[0] ## Offset the time differences - this is printed to screen as "End: $"
	ChapEnd[$CHAPTERS]=$DiscTime ## Add $DiscTime as last array element
	ChapEnd=( "${ChapEnd[@]}" )     ## Pack the array
for ((t=0; t<$CHAPTERS; t++)) 
     do TimeDiff[$t]=$((${ChapTimeEnd[$t]} - ${ChapTimeStart[$t]}))  
Duration=$(echo - |awk '{printf "%02d:%02d:%02d","'"${TimeDiff[$t]}"'"/(60*60),"'"${TimeDiff[$t]}"'"%(60*60)/60,"'"${TimeDiff[$t]}"'"%60}';echo)
echo "Chapter: `printf "%02d" $(($t+1))`  Start: "${ChapStart[$t]}" End: "${ChapEnd[$t]}" Duration: $Duration"
done

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question