Stream manipulation in UNIX shell scripting

i have a file something like this :

start:
01:00:00
01:30:00
02:30:00
05:30:00
end:
01:13:00
02:00:00
02:40:00
05:45:00

and i want (end - start) total run time in below format:

run:
00:13:00
00:30:00
00:10:00
00:15:00

i want to create a shell script for the above problem, please help.
Thanks in advance.

Hello,

Could you please try the following.
Lets say we have your input in file named "date_solution" and I make a script named "date_solution.ksh" which will give us an raw Output. Then I am putting this script's Output to

 sed 

.

Please check this out and let me know if this helps.

Here is the script.

$ cat date_solution.ksh
hun=100
fourty=40
b=`awk '/end\:/,/^$/' date_solution | sed 's/end\://g;s/\://g' | sed '/^$/d'`
a=`awk '/start\:/,/end\:/' date_solution | sed 's/start\://g;s/end\://g;s/\://g' | sed '/^$/d'`
 
#echo $a
#echo $b
k=0
set -A array_a $a
set -A array_b $b
 
for i in ${array_a[@]}
do
l=0
for j in ${array_b[@]}
do
if [[ $k -eq $l ]]
then
#echo $("$j - $i")
w=$(($j - $i))
w=$(($w / $hun))
if [[ $w -ge $fourty ]]
then
w=$(($w - $fourty))
w=$(($w * $hun))
echo $w
else
w=$(($w * $hun))
echo $w
fi
fi
let "l = l +1 "
done
let "k = k + 1"
done
 

Please run it by following command.

$ ksh date_solution.ksh | sed 's/^/00:/g;s/..$/:00/'

Out put will be as follows.

00:13:00
00:30:00
00:10:00
00:15:00
$

Thanks,
R. Singh

thanx ravinder that was very helpful,
i ll surely get back to you when stuck somewhere again :slight_smile:

thanks a lot for the help once again.

awk solution, it is more of tricking mktime and strftime to get the results

awk '/start/{st=1;next} /end/{ed=1;st=0;next} st{ s[x++]=$1;next } ed{ e[y++]=$1;next }
END {
        for(i=0;i<x;i++){
                gsub(/:/," ",s); gsub(/:/," ",e)
                tt1=mktime("2000 01 01 "s)
                tt2=mktime("2000 01 01 "e)
                diff=(tt2-tt1)-14400+43200
                print strftime("%H:%M:%S", diff)
        }
}' input_file

--ahamed

There is so many methods to do it, here one more ksh/bash example. Use only shell, no other commands.

inputfile:

start:
23:55:00
01:00:00
01:30:00
02:30:00
05:30:00
23:00:00
end:
01:00:21
01:13:00
02:00:00
02:40:00
05:45:00
00:45:01
#!/usr/bin/somesh like bash or ksh
nextpart=0  # 1st/2nd section
while read line
do
        case "$line" in
                start*) cnt=0 ; continue ;;
                end*) nextpart=1 ; cnt2=0 ; continue ;;
        esac
        ((cnt+=1))
        
        # parse line to the array arr
        IFS=":" arr=($line)
        # start: section
        [ "$nextpart" = 0 ] && (( start[$cnt]=${arr[0]}*3600+${arr[1]}*60+${arr[2]} )) && continue
       
        # end: section
        ((cnt2+=1))
        (( end=${arr[0]}*3600+${arr[1]}*60+${arr[2]} ))
        starttime=${start[$cnt2]}

        # ex. start 23:xxx , end 01:00 , 24 h
        (( end < starttime )) && ((end=end+24*3600))
        ((diff=end-starttime ))

        (( h=diff/3600 ))
        (( m= (diff- h*3600 )/60 ))
        (( s= diff- h*3600 - m*60  ))

        (( h<10)) && h="0$h"
        (( m<10)) && m="0$m"
        (( s<10)) && s="0$s"
        echo "$h:$m:$s"

done < inputfile