Convert Seconds to hh:mm:ss


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Seconds to hh:mm:ss
# 1  
Old 02-18-2009
Convert Seconds to hh:mm:ss

Hi All
I need to convert a number of fields in a record from seconds to hh:mm:ss ( or possibly hhh:mm:ss ). I'm guessing awk is the way to go .
File has multiple records and each record contains 101 fields - can awk handle that ? The seconds values will be in fields 3 - 101 and could be 0.

Any help would be much appreciated.
# 2  
Old 02-18-2009

Here's the method in the shell. It is easily converted to awk.

Code:
secs=${1:?}

h=$(( secs / 3600 ))
m=$(( ( secs / 60 ) % 60 ))
s=$(( secs % 60 ))

printf "%02d:%02d:%02d\n" $h $m $s

# 3  
Old 02-19-2009
Thank You Mr J.
Actually - I'd got that far - I should have made my question clearer. I can get the right result using script on a single value. My issue is with applying the function to multiple fields in each line of the file.
This is the ksh I had.
cat hms.ksh
---------
Code:
#!/bin/ksh
#convert sec to h:m:s
((hour=$1/3600))
((mins=($1-hour*3600)/60))
((sec=$1-((hour*3600) + (mins*60))))
printf "%02d:%02d:%02d\n" "$hour" "$mins" "$sec"

--------------
Your solution looks a little more elegant for that.

Last edited by rbatte1; 07-05-2016 at 04:50 AM.. Reason: Code tags
# 4  
Old 02-20-2009
Put the conversion into a function.
Read the file line by line.
Parse the line into its components.
Apply the conversion to the appropriate parts.


This should probably be done in awk if the file is more than a few lines.

Last edited by rbatte1; 07-05-2016 at 04:51 AM.. Reason: Remove needless indentation
# 5  
Old 02-20-2009
I'll try that - it can be thousands of lines and in multiple files so I'll have a go in awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert days hours minutes seconds to minutes?

Hi, please help with below time conversion to minutes. one column values: 2 minutes 16 seconds 420 msec 43 seconds 750 msec 0 days 3 hours 29 minutes 58 seconds 480 msec 11 seconds 150 msec I need output in minutes(total elapsed time in minutes) (2 Replies)
Discussion started by: ramu.badugula
2 Replies

2. Shell Programming and Scripting

Convert a future date into epoch seconds on HPUX system

Hi All, I have scenario where i have to compare two dates. I thought of converting them to epoch seconds and do a numeric comparison. This works fine on Linux systems. $ date -d '2015/12/31' +%s 1451538000 $ date +%s 1449159121 But we don't have -d option in HPUX. What would be... (5 Replies)
Discussion started by: veeresh_15
5 Replies

3. Shell Programming and Scripting

Compare fraction number and convert duration to seconds

Hi friends, I have a file with contents below: 01.m4a 00:14:45.82, 01.mp4 00:03:46.05, -659.770000 05.m4a 00:27:43.51, 05.mp4 00:27:45.10, 1.590000 06.m4a 00:11:39.73, 06.mp4 00:11:44.60, 4.870000 If 5th column value more than 3 or less than -3 then I should get its name (from first... (2 Replies)
Discussion started by: magnus29
2 Replies

4. Shell Programming and Scripting

Convert duration of the process to seconds

Hi, I am looking to write a script to kill the process which are running for more than 7 days. So i have a command like "ps -eo pid,etime,args | grep -i xxxx" ( process which has xxx in it and running for more than 7 days needs to be killed ). When i exeucte the above command , i am... (2 Replies)
Discussion started by: forums123456
2 Replies

5. Shell Programming and Scripting

Script to add time convert to seconds

Hi, What i am looking for and i am new to this too, is a bash script that will add time in the format hh:mm:ss and produce the answer in minutes or seconds. It needs to be a loop since there are hundreds of times in my file. This is data is from a CDR that calculates duration of time used. ... (2 Replies)
Discussion started by: trotella
2 Replies

6. Shell Programming and Scripting

awk convert seconds to time of day

Does anyone know of a way to convert "seconds" to time of day in "hh:mm:ss" ? Trying to do in awk with strftime but with no luck. Thanks (2 Replies)
Discussion started by: timj123
2 Replies

7. HP-UX

Ticks in seconds.

Hello all, Is there any thumb rule or aproximation of the equivalence in second of one tick? Thank you in advance. (1 Reply)
Discussion started by: mig28mx
1 Replies

8. Shell Programming and Scripting

Convert minutes to hours, minutes, seconds

How would you convert lets say a 1000 minutes to hours, minutes, seconds (1 Reply)
Discussion started by: Vozx
1 Replies

9. UNIX for Dummies Questions & Answers

seconds to hh:mm:ss

Any sleek way to convert seconds to hh:mm:ss format . I know it can be done by mod and divide . Looking for a one liner if possible . Example 3600 seconds = 01:00:00 3601 seconds = 01:00:01 (2 Replies)
Discussion started by: akrathi
2 Replies

10. UNIX for Advanced & Expert Users

how to get number of seconds

How do I get the number of seconds since 1970, within a script, for the previous day at 23:59? I need this value to pass into a sql statement to cleanup records older than the previous day at midnight. It will be automated via cron so no hard coding allowed. Thanks! (2 Replies)
Discussion started by: captainzeb
2 Replies
Login or Register to Ask a Question