LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2008, 12:41 PM   #1
newbie_adm
Member
 
Registered: Jun 2006
Posts: 40

Rep: Reputation: 15
Calculating seconds


Hi,
I'm a newbie on bash programing, I would like to ask how to compute the time on my asterisk cdr. WHat i would like to do is to compute the total minutes of all answered calls. Actually, i already done that part but what I need is to add or round off the time used to nearest minute. Let's say I a client uses 123 seconds(this is the standard format of asterisk cdr) and I wanted to round this of to nearest minute. If I compute this to minutes, this would be 2 minutes and 3 seconds. What I wanted to do is convert the remaining 3 seconds to minutes or round it off to nearest minutes and that would give me 3 minutes. I'm not sure how to do this. please help me

thanks

Best Regards,
newbie_adm
 
Old 10-20-2008, 12:45 PM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by newbie_adm View Post
If I compute this to minutes, this would be 2 minutes and 3 seconds. What I wanted to do is convert the remaining 3 seconds to minutes or round it off to nearest minutes and that would give me 3 minutes.
I hope this isn't a billing system: where I come from 2 minutes and 3 seconds rounds down to 2 minutes, not up to 3 minutes. (Do you work for a legal firm, by any chance?)
 
Old 10-20-2008, 12:59 PM   #3
newbie_adm
Member
 
Registered: Jun 2006
Posts: 40

Original Poster
Rep: Reputation: 15
Hi,
Nope this is not a billing system.I'm working as a sysad here in Manila. I'm just using this one to match or compare it with our termination partner. My boss wanted to have a email notification of the total minutes that we have used. Our termination partners is billing us on this manner. So i would like to have the same format to compare it with our partner. I hope you could help with this.thanks

Best Regards,
newbie_adm
 
Old 10-20-2008, 01:47 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

the shell normally calculates only with integers. You can use it to get the number of minutes:
Code:
# always round to the previous minute
min=`expr $sec / 60`
# bash:
min=$(($sec / 60))
# always round to the next minute
min=`expr $sec / 60 + 1`
# bash:
min=$(($sec / 60 + 1))
# from 0 .. 29 round to the previous, otherwise to the next minute
min=`expr $sec / 60`
remaining=`expr $sec - $min \* 60`
test $remaining -ge 30 && min=`expr $min + 1`
# bash:
min=$(($sec / 60))
remaining=$(($sec - $min * 60))
test $remaining -ge 30 && min=$(($min + 1))
Jan
 
Old 10-20-2008, 01:56 PM   #5
newbie_adm
Member
 
Registered: Jun 2006
Posts: 40

Original Poster
Rep: Reputation: 15
Hi Jan61,
Thank you very much. Can I also ask if I can use this with to the cdr file of answer.Usually I a line per line calls which means these are total seconds per calls.

exampel file. cdr.txt
15
150
259

Thank you very much

Best Regards,
newbie_adm
 
Old 10-20-2008, 02:07 PM   #6
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

to calculate line per line, you can use a loop:
Code:
while read sec; do
  # do the calculating stuff - e. g.
  echo $(($sec / 60))
done <cdr.txt >out.txt
# or
for sec in `cat cdr.txt`; do
  ...
done >out.txt
To calculate a summary:
Code:
sum=0
while read sec; do
  # do the calculating stuff - e. g.
  sum=$(($sum + $sec / 60))
done <cdr.txt
echo $sum
# or
for sec in `cat cdr.txt`; do
  ...
done
echo $sum
Jan
 
Old 10-21-2008, 05:15 AM   #7
newbie_adm
Member
 
Registered: Jun 2006
Posts: 40

Original Poster
Rep: Reputation: 15
Hi
First of all I wanted to thank you for helping me out. I have made the script working but there is one problem. I've tried to redirect the result in to a file but the only thing that was being printed on the file was the last line. I wanted to get all the lines or minutes in evry line to another file. Below is my simple script. When I tried the echo command, it was successful, i was able to retrieved 5 lines of the t3.txt(I have 5 lines that was in seconds and converted to minutes using this script). But when I tried using the printf the result was that the last entry of thiis result was printed into file.



#!/bin/bash

seconds=0
for i in `cat t3.txt`;
do
seconds=$((i + seconds / 60 ))
minutes=$(( seconds / 60 + 1))
seconds=$(( seconds % 60 ))
echo "$minutes"

done


Best Regards,
newbie_adm
 
Old 10-21-2008, 11:56 AM   #8
newbie_adm
Member
 
Registered: Jun 2006
Posts: 40

Original Poster
Rep: Reputation: 15
Hi,

ANy inputs,Please help me I really need it very badly.thanks

Best Regards,
newbie_adm
 
Old 10-21-2008, 12:29 PM   #9
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

using printf you must keep in mind, that you have to add a "\n" to your format string, printf does not add a newline per default (echo does that). How do you redirect your output? If you do it in the echo or printf line, you must use ">>file" (append), otherwise every new loop will overwrite the file.

Jan
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert total no of seconds in the format hour minutes and seconds suchi_s Programming 15 03-15-2011 11:34 AM
calculating throughput ilnli Programming 6 07-17-2007 01:58 AM
calculating in bash ovince Programming 3 03-28-2007 10:50 AM
Calculating checksum of a structure george_mercury Programming 3 01-09-2005 05:07 AM
Calculating Surface Distance! john23 Programming 1 11-27-2003 01:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration