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

Awk - reading in HH:MM:SS

Status
Not open for further replies.

AwkRookie

Programmer
Oct 15, 2002
10
US
This is another post of the same code I've posted before with a different slant and some more verbage below it:

I'm attempting to run the following script from p.68 of the Aho, Kernighan, Weinberger bible:

{ for (i=1; i <=NF; i++)
sum += $i
if (NF > maxfld)
maxfld = NF
}
END { for (i = 1; i <= maxfld; i++) {
printf(&quot;%g&quot;, sum)
if (i < maxfld)
printf(&quot;\t&quot;)
else
printf(&quot;\n&quot;)
}
}

My data (which I cannot post because it is sensitive data) is 8 columns of double-precision, 32-bit words. The data had a 9th column which was time in HH:MM:SS but since I didn't know how to handle this in Awk I printed out the remaining fields to a file and piped the file to the code above (which I call ex31_input) as input. I have some time on my hands at the moment so I can probably create a sample data file to post as well; I'll try to post the sample file in a day or two.

Forgetting the index value for sum (should be sum, as it is above) may have caused my code to return nothing. It is supposed to return nothing if the input file is empty; it obviously is not empty. Anyhow, assuming that I fix the sum to read sum, how can I make this script run for any number of numeric fields (8 in my example with a time stamp as the first field = 9 fields total) with the first field is in HH:MM:SS?

Thanks for everybody's help with my other posts - they are my first to this board. I keep the responses in an Awk notebook so if I go a few months without having to use Awk (which can happen in my line of work) I have a reference to go back to.
 
post an example - this is GIGA confusing ;)
post your desired output as well. vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
AwkRookie:

Since awk arrays are associative, you can sum up even garbage data
such as a time string. I'm making an assumption that you want to
keep the spacing the same, but don't print the garbage data.
Define an array with the indices you want to skip; In the END
section, if the index is to be skipped print a tab else the element.

For example, if data.file looks like:

12:46:09 1 2 3 22:46:15 4
13:52:32 5 6 7 19:46:15 8

and indices 1 and 5 are to be skipped:

Regards,

Ed

awk ' BEGIN { skipit[1]=1; skipit[5]=1 }
{
for (i=1; i <=NF; i++)
sum += $i
if (NF > maxfld)
maxfld = NF
}
END { for (i=1; i <= maxfld; i++)
{
# if you skip a field, print a tab
# else print the sum element
if(i in skipit)
printf(&quot;\t&quot;)
else
printf(&quot;%g&quot;, sum)
if (i < maxfld)
printf(&quot;\t&quot;)
else
printf(&quot;\n&quot;)
}
} ' data.file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top