add up time with xx:yy format in bash how?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add up time with xx:yy format in bash how?
# 1  
Old 02-19-2012
add up time with xx:yy format in bash how?

Hi,
How can I add up a series of string like:
Quote:
1:11 0:13 2:06 1:38 1:36 0:06 0:31 0:33 0:38 0:44
and get a total as hour and minutes in a format like this:
Quote:
3hrs 12min
# 2  
Old 02-19-2012
All in shell...

Code:
list="1:11 0:13 2:06 1:38 1:36 0:06 0:31 0:33 0:38 0:44"

h=0
m=0
for x in ${list}
do
    h=$(( h + ${x%%:*} ))   # add hours and minutes
    m=$(( m + ${x##*:} ))
done

h=$(( h + (m /60) ))    # minutes are likely more than 60, calc hours and add in 
m=$(( m % 60 ))     # adjust minutes

echo "${h}hrs ${m}min"


Last edited by agama; 02-19-2012 at 07:08 PM.. Reason: typo
# 3  
Old 02-19-2012
Quote:
Originally Posted by agama
All in shell...

Code:
list="1:11 0:13 2:06 1:38 1:36 0:06 0:31 0:33 0:38 0:44"

h=0
m=0
for x in ${list}
do
    h=$(( h + ${x%%:*} ))   # add hours and minutes
    m=$(( m + ${x##*:} ))
done

h=$(( h + (m /60) ))    # minutes are likely more than 60, calc hours and add in 
m=$(( m % 60 ))     # adjust minutes

echo "${h}hrs ${m}min"

To learn more...
1- Could you tell me how I can learn use of Regular Expressions?

2- Would you explain these two:
Quote:
h=$(( h + ${x%%:*} )) # add hours and minutes
m=$(( m + ${x##*:} ))
3- I get the following error for some users and for some I don't!
Quote:
./timeon: line 82: m + 08: value too great for base (error token is "08")
---------- Post updated at 09:26 PM ---------- Previous update was at 09:18 PM ----------

I fixed it. it is base 8 and I need to force bash to recognize these number as decimal by using 10# in front of the number!
# 4  
Old 02-19-2012
Don't confuse the shell variable expansion substitution syntax with regular expressions because they are (just as confusing at first) and quite different.

For the full list of possibilities have a look at one of the shell man pages, for example the kshell page:
man/man1/ksh.html man page

The parameter expansion is about 25% down in the page (search for %% and you'll find it).

Specifically. ${name%%:*} expands the value in the variable 'name' and matches the longest pattern ":*" that is at the end of the string. The part of the expansion that is matched is truncated. So, if name contains ab:cd:ef, the expression above would match ":cd:ef" and delete that leaving just "ab".

The ${name:##*:} does the same kind of truncation, but starting from the beginning of the string. Again, if name contains "ab:cd:ef" then only "ef" would be left as the longest pattern from the beginning to ':' is "ab:cd:".

As for your error, it's treating the leading 0 as an indicator that the value should be an octal number. I don't usually work in bash -- Kshell's evaluation supports floating point calculations and doesn't make this assumption, so I wasn't aware that this would be an issue. You can compensate for this behaivour with this:

Code:
   h=$(( ${h#0} + ${x%%:*} ))  
   m=$(( ${m#0} + ${x##*:} ))

This is similar to the examples I gave above, but it matches the shortest pattern starting at the beginning of the line. So if h == "04" the expansion will just be 4, and bash won't complain. If h == "00" the expansion will be 0 which again will be fine. Finally, if h == "12" there isn't a match so there isn't a truncation.

Good luck!

---------- Post updated at 21:40 ---------- Previous update was at 21:39 ----------

Quote:
Originally Posted by bashily
I fixed it. it is base 8 and I need to force bash to recognize these number as decimal by using 10# in front of the number!
Nice job!!

That happens to be a trick that I didn't know -- I approached it a bit differently, so as usual, I learned something too.
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

2. Post Here to Contact Site Administrators and Moderators

Display time in 12 hr format

Write a script named time that displays the time in standard 12-hour format, rather than 24-hour format. Allow the user to give a -m option to get 24-hour format. For example: > date Sun Feb 10 10:56:50 CST 2008 > time 10:56 AM > date Sun Feb 10 21:57:07 CST 2008 > time 9:57 PM >... (0 Replies)
Discussion started by: satish24
0 Replies

3. Shell Programming and Scripting

How to add missing date and time in a bash script?

Hi Again, I have a file that contains date and time for the past 2 hours. What i need is add missing date and time in a file. INPUT 2016-01-13 01:33 10 2016-01-13 01:31 10 2016-01-13 01:30 10 2016-01-13 01:29 10 2016-01-13 01:28 10 2016-01-13 01:27 10 2016-01-13 01:26 10 2016-01-13... (14 Replies)
Discussion started by: ernesto
14 Replies

4. Shell Programming and Scripting

time format

Hello Guys. I have copied the following from the time man pages time -f "%E real,%U user,%S sys" ls -Fs But I am getting -f: command not found Regards (3 Replies)
Discussion started by: fdc2suxs
3 Replies

5. Shell Programming and Scripting

Date and Time format

I want to append current date and time to a file name like filename_090920091210. If I use filename_`date +%d%m%Y%T`, the ouput is filename_0909200912:10:33. How to format this as filename_090920091210. Please let me know. Thanks in advance. (3 Replies)
Discussion started by: srimenon09
3 Replies

6. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

7. Shell Programming and Scripting

help in time format

how to grep 30 mins from starting time of script let for example,script runs at 02:00am....i want to grep from 01.30 till 02:00 ------------------------------------------------------------------- how to achive this in a k-shell script ? (3 Replies)
Discussion started by: ali560045
3 Replies

8. Shell Programming and Scripting

time format..

HI.. I have some files...when doing "ls -l" its like this.. -rwxr-xr-x 1 e2e e2e 747 Aug 30 15:18 abc.txt how can I get the number YYYYMMDD from this...( since I need to compare this number with some other value..) with the help of date/awk/sed/epoch or whatever u... (1 Reply)
Discussion started by: clx
1 Replies

9. Shell Programming and Scripting

replace time format

Hello All, I have a problem with the following text file. For the field number 5 which is the time format (hh:mm:ss). But I would like to delete "ss" and showing hh:mm only. 00001,CLIENT,Company,1218,N,1:04,35,0.211,0,0.211,1.155531,0:00,0,0,0,0,0,1:04,35,0.211,0,0.211,1.155531,foold... (16 Replies)
Discussion started by: happyv
16 Replies

10. Shell Programming and Scripting

Please help formatting bash "time" variable to HH:MM:SS format

Ok, this is going to be hard to describe, but here it goes. I have written a bash script that, while executing starts a timer, and when done stops the timer. The $RUNTIME variable value is in seconds, so the variable usually equals a number like 126 (equals 2 minutes 6 seconds). In my script I... (3 Replies)
Discussion started by: vikingshelmut
3 Replies
Login or Register to Ask a Question