Note that I used a Korn shell in post #2 and itkamaraj used bash in post #3. If you use bash, you will need to strip leading zeros from minutes and seconds values 08 and 09. The Korn shell treats those strings as decimal 8 and 9, respectively; but bash treats them as invalid octal numbers.
You can also do this just using variable expansions without resorting to using cut to extract the minutes with something more like:
Code:
#!/bin/bash
list="1:51:59 0:33:08 2:06:41"
h=0
m=0
s=0
for x in ${list}
do
echo ${x}
S=${x##*:} # Extract seconds.
x=${x%:$S} # Strip seconds from x.
S=${S#0} # Strip leading 0 from S.
M=${x##*:} # Extract minutes.
M=${M#0} # Strip leading 0 from M.
H=${x%:*} # Extract hours (assume there is no leading 0 here.
h=$(( h + H ))
m=$(( m + M ))
s=$(( s + S ))
#echo "H : ${h}"
#echo "M : ${m}"
#echo "S : ${s}"
done
m=$(( m + (s/60) ))
h=$(( h + (m/60) ))
m=$(( m % 60 ))
s=$(( s % 60 ))
echo "${h}hrs ${m}min ${s}sec"
which produces the following output when run with either bash or ksh:
Code:
1:51:59
0:33:08
2:06:41
4hrs 31min 48sec
Note that I modified the time values in $list to provoke bash complaints with bad octal values. If we change the code in post #3 to use the above list (and add the missing " at the end of the echo statement on the last line), we get the output:
Code:
1:51:59
0:33:08
script_name: line 14: s + 08: value too great for base (error token is "08")
2hrs 24min 59sec
These 3 Users Gave Thanks to Don Cragun For This Post:
Hello everyone and let me start off by thanking anyone who can help with this.
I work for a company that uses Unix as one of their servers. I'm not at all familar with Unix beyond logging after I restart the server:rolleyes: I'm looking for some command that will bring me up a list of current... (3 Replies)
Hi all,
I have a list of times:
...10:02
15:34
20:05
01:51
06:55
09:00
05:52...
That's just part of the list (its huge).
How do I go about selecting certain times, e.g. just between 23:00 and 05:00 ?? (4 Replies)
i have a list of numbers like this;
124
235
764
782
765
451
983
909
...
and i want to make a sum with the first 3 of them then the next 3 and so on.
124+235+764=1123
782+765+451=1998
...
some ideas? (4 Replies)
Hi Experts,
I want to add 20% to the values and get an output , please advise with script , awk etc,
# cat datafile.txt
50.4053
278.383
258.164
198.743
4657.66
12.7441
646.787
1.56836
23.2969
191.805
53.3096
1.12988
999.058
4100.29 (2 Replies)
If I had a word list with a large amount of words in it, how would I (using a unix command) add, say, 123 to the end of each word?
EDIT: The word list is stored in a large text file. I need a command that applies the ending to each word in the file and saves the result in a new text file. (7 Replies)
Hello,
i'm trying to implement the times() function and i'm programming in C.
I'm using the "struct tms" structure which consists of the fields:
The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process.
The tms_stime structure... (1 Reply)
I'm having some troubles setting an instance of postgreSQL to automatically start upon system boot. I have two servers running this app, one is automatically starting the service, the other is not. I'm attempting to use the "svcadmin" command, however, apparently when I run a "svcs -a" search, the... (6 Replies)
Hi All,
I have a file with long list of numbers. This file contains only one column. These numbers are very large. I am using following command:
cat myfile.txt | awk '{ sum+=$1} END {print sum}'
The output is coming in scientific notation. How do I get the result in proper format?
... (4 Replies)