Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK hhmmss

Status
Not open for further replies.

revjax85

Technical User
Jun 12, 2009
1
0
0
Hello,
I have a file that contains the following:

034023 052030
051811 061150
061711 091050
071811 111150
031811 151150
091811 123412
060021 180042
123500 142832

I need to convert these times into seconds, subtract field 2 from field 1, and then convert it back to hh:mm:ss.
This is what I have so far any help would be greatly appreciated:

BEGIN {print "Time 1""\t""\t""Time 2""\t""\t""Time 3"}

{
s1=substr(s,1,2)
s2=substr(s,3,2)
s3=substr(s,5,2)
s=sprintf("%s:%s:%s",s1,s2,s3)
}
{print $0 s1*3600,s2*60,s3 }
END{}
 
Hi

revjax85 said:
s1=substr(s,1,2)
s2=substr(s,3,2)
s3=substr(s,5,2)
What is s and where it gets its value ?

Anyway, as far as I understand it, you want something like this :
Code:
[red]BEGIN[/red] [teal]{[/teal]
  print [green][i]"Time 1[/i][/green][lime][i]\t[/i][/lime][green][i]Time 2[/i][/green][lime][i]\t[/i][/lime][green][i]Time 3"[/i][/green]
[teal]}[/teal]
[teal]{[/teal]
  diff[teal]=[/teal]sincemidnight[teal]([/teal][navy]$2[/navy][teal])-[/teal]sincemidnight[teal]([/teal][navy]$1[/navy][teal])[/teal]
  printf[teal]([/teal][green][i]"%s[/i][/green][lime][i]\t[/i][/lime][green][i]%s[/i][/green][lime][i]\t[/i][/lime][green][i]%02d:%02d:%02d[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal],[/teal][navy]$1[/navy][teal],[/teal][navy]$2[/navy][teal],[/teal]diff[teal]/[/teal][purple]60[/purple][teal]/[/teal][purple]60[/purple][teal],[/teal]diff[teal]/[/teal][purple]60[/purple][teal]%[/teal][purple]60[/purple][teal],[/teal]diff[teal]%[/teal][purple]60[/purple][teal])[/teal]
[teal]}[/teal]
[b]function[/b] sincemidnight[teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
  return substr[teal]([/teal]what[teal],[/teal][purple]1[/purple][teal],[/teal][purple]2[/purple][teal])*[/teal][purple]60[/purple][teal]*[/teal][purple]60[/purple][teal]+[/teal]substr[teal]([/teal]what[teal],[/teal][purple]3[/purple][teal],[/teal][purple]2[/purple][teal])*[/teal][purple]60[/purple][teal]+[/teal]substr[teal]([/teal]what[teal],[/teal][purple]5[/purple][teal],[/teal][purple]2[/purple][teal])[/teal]
[teal]}[/teal]
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

By the way, next time ask your [tt]awk[/tt] questions in forum271.

And next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top