Sponsored Content
Top Forums Shell Programming and Scripting Stream manipulation in UNIX shell scripting Post 302842567 by kshji on Friday 9th of August 2013 12:59:23 PM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PARSE_STR(3)								 1							      PARSE_STR(3)

parse_str - Parses the string into variables

SYNOPSIS
void parse_str (string $str, [array &$arr]) DESCRIPTION
Parses $str as if it were the query string passed via a URL and sets variables in the current scope. Note To get the current QUERY_STRING, you may use the variable $_SERVER['QUERY_STRING']. Also, you may want to read the section on vari- ables from external sources. Note The magic_quotes_gpc setting affects the output of this function, as parse_str(3) uses the same mechanism that PHP uses to populate the $_GET, $_POST, etc. variables. PARAMETERS
o $str - The input string. o $arr - If the second parameter $arr is present, variables are stored in this variable as array elements instead. RETURN VALUES
No value is returned. EXAMPLES
Example #1 Using parse_str(3) <?php $str = "first=value&arr[]=foo+bar&arr[]=baz"; parse_str($str); echo $first; // value echo $arr[0]; // foo bar echo $arr[1]; // baz parse_str($str, $output); echo $output['first']; // value echo $output['arr'][0]; // foo bar echo $output['arr'][1]; // baz ?> SEE ALSO
parse_url(3), pathinfo(3), http_build_query(3), get_magic_quotes_gpc(3), urldecode(3). PHP Documentation Group PARSE_STR(3)
All times are GMT -4. The time now is 05:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy