![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to assign a variable value to array? | balamv | Shell Programming and Scripting | 2 | 06-06-2008 12:33 AM |
| split variable values into array | finalight | Shell Programming and Scripting | 4 | 05-21-2008 12:21 AM |
| Help in passing array of inputs to C program using script? | ahjiefreak | Shell Programming and Scripting | 1 | 03-20-2008 04:36 AM |
| creating array variable | scriptingmani | Shell Programming and Scripting | 2 | 06-28-2007 07:25 AM |
| Array help (variable substitution). | dsimpg1 | Shell Programming and Scripting | 3 | 04-11-2007 01:38 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
AWK program with array variable
Hi,
I made a small awk program just to test array variables. I couldn't find anything wrong with it. But it doesn't give out valid numbers.( just 0.00 ) Do you see any problem that I didn't see? Thanks in advance! Here is the program: ################################## BEGIN { FS = "," } { job = $2 station = $3 time_period = substr($6,1,3) wait= $11 if (station > 0) { while ( y <=24 ) { ++y if ( time_period == y ) { hour[y] += wait ++i[y] } } } } END { y = 0 while ( z <= 24 ) { ++z ++y if ( i[y] > 0 ) { AVG[z]=hour[y]/i[y] printf("%9.2f\n",AVG[z]) } } } ################################### Here is one row of my data file: 17.11.2004,5189400,222,12538476808, 215, 23:51:01, 23:51:14, 23:51:27, 13, 0, 13, d, 1, 0, 20718, (253)847-6, 1303, -1, 23:51:04, 23:51:04, 0, 23, 3, 1, 2, 0, respo, DEL Last edited by whatisthis; 05-10-2005 at 05:29 AM. Reason: typo |
| Forum Sponsor | ||
|
|
|
#2
|
|||||
|
|||||
|
Quote:
Quote:
Quote:
the substring of legth 3 starting from the 1th location is '23:' Quote:
Is that what you want? Quote:
|
|
#3
|
||||
|
||||
|
typo on the post
vgersh99,
Another Begin block is a typo which shouldn't be there. This program is to print out AVG $11 hourly. substr($6,1,3) represents hourly time(01 ~24). I only need $11 field if $3 is greater than 0. So if $3 > 0 and if substr($6,1,3)=01, then hour[01] += $11; if substr($6,1,3)=12, then hour[12] +=$11.... At the end block, I need to AVG array "hour[01]" ~"hour[24]" individually and print them out. I hope I made myself clear. ?? |
|
#4
|
||||
|
||||
|
Quote:
nawk -f what.awk myDataFile.txt what.awk: Code:
BEGIN {
FS=","
}
$3 > 0 {
h=substr($6, 1, index($6, ":")-1)
hourA[h] += $11
hourAc[h]++
}
END {
for (i in hourA)
printf("hour->[%d] value->[%d] hourAc->[%d] avg->[%.2f]\n", i, hourA[i], hourAc[i], hourA[h] / hourAc[h])
}
Last edited by vgersh99; 05-11-2005 at 07:07 AM. |
|
#5
|
||||
|
||||
|
add 0 at the end
I got the problem solved by adding a + 0 at the end of time_period=substr($6,1,3)
time_period = substr($6,1,3) +0 I guess if I don't add +0, the express if evaluated as string and the program will never go to if statement. Thanks for all the help!! |
||||
| Google The UNIX and Linux Forums |