Stream manipulation in UNIX shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stream manipulation in UNIX shell scripting
# 1  
Old 08-09-2013
Stream manipulation in UNIX shell scripting

i have a file something like this :
Code:
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:
Code:
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.

Last edited by Scott; 08-09-2013 at 10:25 AM.. Reason: Code tags
# 2  
Old 08-09-2013
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
Code:
 sed

.


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



Here is the script.

Code:
$ 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.


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

Out put will be as follows.

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



Thanks,
R. Singh

Last edited by RavinderSingh13; 08-09-2013 at 11:45 AM.. Reason: a bit change in script.
# 3  
Old 08-09-2013
thanx ravinder that was very helpful,
i ll surely get back to you when stuck somewhere again Smilie

thanks a lot for the help once again.
# 4  
Old 08-09-2013
awk solution, it is more of tricking mktime and strftime to get the results
Code:
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[i]); gsub(/:/," ",e[i])
                tt1=mktime("2000 01 01 "s[i])
                tt2=mktime("2000 01 01 "e[i])
                diff=(tt2-tt1)-14400+43200
                print strftime("%H:%M:%S", diff)
        }
}' input_file

--ahamed
# 5  
Old 08-09-2013
There is so many methods to do it, here one more ksh/bash example. Use only shell, no other commands.

inputfile:
Code:
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

Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

unix Shell scripting

Hi All, need help to complete the automation but stuck at a perticular situation below is the code <code> fixed_function_name { code.... code.... variable_map= { a="/a" b="/b" c="/c" so on... } (7 Replies)
Discussion started by: yadavricky
7 Replies

2. Shell Programming and Scripting

Shell Scripting Date manipulation

Hi Experts, i have date as inputdate=01/01/2013,how to get the previous date from this date and also first day's date of the month. example: inputdate=01/06/2013 previousdate=31/05/2013 firstdate=01/05/2013 how can i get solution to this. my unix is not supporting GNU Dates ... (0 Replies)
Discussion started by: learner24
0 Replies

3. UNIX for Dummies Questions & Answers

Unix Shell Scripting( Calling from Unix to PLSQL)

Hello Experts, I have the following questions to be discussed here at this esteemed discussion forum. I have two Excel sheets which contain Unix Commands llike creating directory the structure/ftp/Copy/Zip etc to basically create an environment. I need help in understanding some of... (1 Reply)
Discussion started by: faizsaadq
1 Replies

4. Shell Programming and Scripting

Unix shell scripting help

Hi , I am beginner in Unix. I have a string /aa/bb/cc/dd. I want to extract the sub string /aa/bb/cc from this . How do i do that in bash? (1 Reply)
Discussion started by: vignesh53
1 Replies

5. Shell Programming and Scripting

Shell scripting string manipulation

Hi, if I have a string delimited by commas how can I put each character on a new line followed by a carriage return, and output this to a filee.g from: s,t,r,i,n,g to s t r i n g thanks you (3 Replies)
Discussion started by: Wahmed9
3 Replies

6. UNIX for Advanced & Expert Users

Need your Help on Unix Shell Scripting.........

Hi Friends, 1. Bash Shell Scrpt to take backup at evening 2. I need a bash shell script for killing all processes. (5 Replies)
Discussion started by: vinayraj
5 Replies

7. HP-UX

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (1 Reply)
Discussion started by: bosskr
1 Replies

8. Shell Programming and Scripting

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (0 Replies)
Discussion started by: bosskr
0 Replies

9. Shell Programming and Scripting

Unix Shell Novice - File Manipulation

Hi, I am brand new to unix and am hoping someone can start me in the right direction. I want to be able to put the results of a file command such as wc -l filename into a variable which I can then use to test against another variable...i.e. I need to show the nth line of a file, but need to... (3 Replies)
Discussion started by: nmullane
3 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question