LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-24-2011, 02:23 AM   #1
thomsa
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Rep: Reputation: 0
adding time in hh:mm:ss format


One of my scripts generated an output like this:
0:0:35
0:0:36
0:0:28
0:0:37
0:0:43
0:1:2

This is actually in a time format(hh:mm:ss) and how do i add all the values and get the sum in seconds .
the output should be in seconds like
total number of seconds : 241 secs
 
Old 02-24-2011, 03:31 AM   #2
JimmyKrak
LQ Newbie
 
Registered: Feb 2011
Posts: 13

Rep: Reputation: 0
time="1:2:3"

let h=`echo $time|cut -d":" -f1
let m=`echo $time|cut -d":" -f2
let s=`echo $time|cut -d":" -f3

let seconds=$h*3600+$m*60+$s
 
Old 02-24-2011, 03:53 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Code:
#!/bin/bash

awk -F: 'BEGIN { totSecs = 0 }
{ totSecs = totSecs + 3600 * $1 + 60 * $2 + $3 }
END { print "Total number of seconds: " totSecs }
' infile
infile holds the output of the script.

Example run:
Code:
$ cat infile
0:0:35
0:0:36
0:0:28
0:0:37
0:0:43
0:1:2

$ ./total.secs.sh
Total number of seconds: 241
@JimmyKrak: The first three let statements are missing a closing `

Hope this helps.
 
1 members found this post helpful.
Old 02-24-2011, 03:55 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In alternative to what suggested by JimmyKrak, you can spare the cut commands to do the conversion:
Code:
echo $time | awk -F: '{print $1*3600 + $2*60 + $3}'
If you want to retrieve the sum after the script has terminated:
Code:
yourscript.sh | awk -F: '{sum += $1*3600 + $2*60 + $3} END{print sum}'
Edit: beaten by druuna!
 
Old 02-24-2011, 04:50 AM   #5
JimmyKrak
LQ Newbie
 
Registered: Feb 2011
Posts: 13

Rep: Reputation: 0
druuna, You're right. That's what I get for not testing better. Thanks.
 
Old 02-24-2011, 06:20 AM   #6
thomsa
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by druuna View Post
Hi,

Code:
#!/bin/bash

awk -F: 'BEGIN { totSecs = 0 }
{ totSecs = totSecs + 3600 * $1 + 60 * $2 + $3 }
END { print "Total number of seconds: " totSecs }
' infile
infile holds the output of the script.

Example run:
Code:
$ cat infile
0:0:35
0:0:36
0:0:28
0:0:37
0:0:43
0:1:2

$ ./total.secs.sh
Total number of seconds: 241
@JimmyKrak: The first three let statements are missing a closing `

Hope this helps.
thank you ...it works
 
Old 02-24-2011, 06:27 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
  


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
[SOLVED] change one time format to other format himu3118 Linux - Newbie 4 06-21-2010 07:29 AM
adding time zone files to the time GUI interface noir911 Red Hat 1 03-08-2010 08:18 AM
GKrellM time format for AM/PM in the time? peteyperson Linux - Software 6 02-21-2009 02:38 PM
Time format in boot-time messages jonr Linux - Kernel 1 12-22-2006 12:31 PM
was able to partition AND format this time!!....BUT..... beginnerx Linux - Newbie 13 10-02-2003 02:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:20 AM.

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