The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-09-2005
whatisthis's Avatar
Registered User
 

Join Date: Aug 2004
Posts: 175
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
Reply With Quote
Forum Sponsor
  #2  
Old 05-09-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by whatisthis
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 = ","
total_wait = 0
you don't need to initialize to 0 - awk variables are set to 0 implicitly
Quote:
Originally Posted by whatisthis
}
{
job = $2
station = $3
time_period = substr($6,1,3)
wait= $11
if (station > 0) {
BEGIN {
why another embedded BEGIN block?
Quote:
Originally Posted by whatisthis
FS = ","
total_wait = 0
}
{
job = $2
station = $3
time_period = substr($6,1,3)
the value of the sixth field is: '23:51:01'
the substring of legth 3 starting from the 1th location is '23:'
Quote:
Originally Posted by whatisthis
wait= $11
if (station > 0) {
while ( y <=24 ) {
++y
if ( time_period == y ) {
hour[y] += wait
++i[y]
}

}

}
}
This END block will be exucuted ONCE after ALL the records in ALL the files are processed.
Is that what you want?
Quote:
Originally Posted by whatisthis
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
What are you trying to do - I don't quote folow the logic here?
Reply With Quote
  #3  
Old 05-10-2005
whatisthis's Avatar
Registered User
 

Join Date: Aug 2004
Posts: 175
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.
??
Reply With Quote
  #4  
Old 05-11-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by whatisthis
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.
??
whis is osmething to start with - you can massage the code below based on your own testing/requirements.

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.
Reply With Quote
  #5  
Old 05-18-2005
whatisthis's Avatar
Registered User
 

Join Date: Aug 2004
Posts: 175
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!!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0