[wplug] converting HH:MM:SS to count of seconds

Patrick Wagstrom pwagstro at andrew.cmu.edu
Mon Dec 20 09:38:39 EST 2004


Alex,

Not sure if this entirely what you're looking for, but here's a little
python script that can do it for you:

---SNIP---
#!/usr/bin/python
import sys

b = sys.stdin.readlines()
for a in b:
    sp = a.split("%")
    s4 = sp[4].split(":")
    sp[4] = str(int(s4[0])*3600+int(s4[1])*60 + int(s4[2]))
    print "%".join(sp).strip()
---SNIP---

Takes the input on stdin, spits it out on stdout.

--Patrick

On Mon, 2004-12-20 at 16:07 +0200, Alexandros Papadopoulos wrote:
> Dear all
> 
> I have an ASCII file with lots (tens of thousands) of lines like these:
> 01.11.04%13:35:51%1%847%00:00:02%141                  % 2
> 01.11.04%13:36:33%5%841%00:00:27%2102319811           % 1
> 01.11.04%13:36:35%1%847%00:00:03%141                  % 2
> 01.11.04%13:37:16%1%841%00:00:11%2103645318           % 1
> 01.11.04%13:37:32%5%837%00:00:58%2102729811           %35
> 
> I'd like to convert the 5th field, as delimited by %'s (which is 
> duration of a phone call) to a single number, designating seconds of 
> duration.
> 
> Hence:
> 00:00:02 becomes 2
> 00:04:31 becomes 271  (0*60^2 + 4*60^1 + 31*60^0)
> 02:00:05 becomes 7205 (2*60^2 + 0*60^1 + 5*60^0)
> and so on.
> 
> Can anyone suggest a way to accomplish this with standard UNIX tools? 
> (bc, cut, perl, grep & friends)
> 
> Thanks
> 
> -A
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug
> 



More information about the wplug mailing list