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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading in HH:MM:SS versus ignoring it (doing ops on HH,MM,SS)

Status
Not open for further replies.

AwkRookie

Programmer
Oct 15, 2002
10
US
Now that I am able to read in a file with HH:MM:SS values (the first element in each row), if I want to perform operations on the hours, minutes, and seconds, how do I read in hours, minutes, and seconds separately, while still reading in all of the other numeric fields in each row?

For example, with the data

16:40:15 1 2 3 4 5
16:40:35 6 7 8 9 10

I now want to define a new field called "seconds" and set it equal to hours*3600+minutes*60+seconds so that I can display the HH:MM:SS as just seconds (this allows me to do interpolation if needed on the other fields).

I'll end up with,

16 40 15 60015 1 2 3 4 5
16 40 35 60035 6 7 8 9 10

Which is, in order of column vectors, HOURS|MINUTES|SECONDS|TOTAL SECONDS|<value #1>|<value #2>|<value #3>|<value #4>|<value #5>| ...
 

nawk -f time.awk time.txt
#------------------------ time.awk------------------
BEGIN {
FLDtime=1
secM=60
secH=secM*60
secD=secH*24
}

{
split($FLDtime, timeARR, &quot;:&quot;);
totalSEC=(timeARR[1]*secH) + (timeARR[2]*secM) + timeARR[3];

gsub(&quot;:&quot;, FS, $FLDtime)

$FLDtime=$FLDtime FS totalSEC

print
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Use

{
split($1,a,&quot;:&quot;)
print a[1],a[2],a[3],a[1]*3600+a[2]*60+a[3],$2,$3,$4,$5,$6
} CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top