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

Bryon Gill bgtrio at yahoo.com
Mon Dec 20 09:47:29 EST 2004


In python I'd do something like

#!/usr/bin/python
import sys
i = file(sys.argv[1],"r")
o = file("/tmp/outfile","w")

for line in i.readlines():
     fields = line.split("%")
     if len(fields) > 1:
         calltime = fields[4] # starts at 0
         callfields = calltime.split(":")
         callseconds = ((int(callfields[0]) * 60 * 60) +
                        (int(callfields[1]) * 60) +
                        int(callfields[2]))
         # assume you want to preserve the other fields
         fixedline = ("%".join(fields[0:3]) + "%" + str(callseconds) + "%" +
                      "%".join(fields[5:]))
     else:
         #don't touch if not enough fields
         fixedline = line
     o.write(fixedline)

It's extra-verbose for readability and you may have to buffer the line reads if 
the file is too big, but I think it does what you want.

Bryon


On Mon, 20 Dec 2004, 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