AWK program with array variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK program with array variable
# 1  
Old 05-09-2005
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 09:29 AM.. Reason: typo
# 2  
Old 05-09-2005
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?
# 3  
Old 05-10-2005
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  
Old 05-11-2005
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 11:07 AM..
# 5  
Old 05-18-2005
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!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with awk array when loading from shell variable

Hi, I have a problem with awk array when iam trying to use awk in solaris box as below..Iam unable to figure out the problem.. Need your help. is there any alternative to make it in arrays from variable values nawk 'BEGIN {SUBSEP=" "; split("101880|110045 101887|110045 101896|110045... (9 Replies)
Discussion started by: cskumar
9 Replies

2. Shell Programming and Scripting

Pass awk array variable to shell

Hi, all suppose I have following myfile (delimited by tab) aa bb cc dd ee ffand I have following awk command: awk 'BEGIN{FS="\t"}{AwkArrayVar_1=$1;AwkArrayVar_2=$2};END{for(i=0; i<NR; i++) print i, AwkArrayVar_1, AwkArrayVar_2,}' myfileMy question is: how can I assign the awk array... (7 Replies)
Discussion started by: littlewenwen
7 Replies

3. UNIX for Dummies Questions & Answers

is there a way to assing variable a value that is output of a program in awk script?

Hi there is there a way to assing variable a value that is output of a program in awk script. For e.g., I did temp=(`grep "" $5 | cut -f8 -d' '`) but it does not work. Any advice??? Thanks in advance!!! :) (3 Replies)
Discussion started by: FUTURE_EINSTEIN
3 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

Using Shell variable within awk program

I have a fixed width text file data.txt delimited by pipe. My requirement is to replace the second column values by *. Problem is that when tried access the shell variable in the awk program, value of shell variable is not printed instead only the name of the variable is printed. Please help to... (4 Replies)
Discussion started by: VijayakumarS
4 Replies

6. Shell Programming and Scripting

Awk multiple variable array: comparison

Foo.txt 20 40 57 50 22 51 66 26 17 15 63 18 80 46 78 99 87 2 14 14 51 47 49 100 58 Bar.txt 20 22 51 15 63 78 99 55 51 58 How to get output using awk 20 22 57 50 51 15 26 17 63 78 80 46 99 55 - - 51 58 49 100 (5 Replies)
Discussion started by: genehunter
5 Replies

7. Shell Programming and Scripting

AWK help: how to compare array elements against a variable

i have an array call ignore. it is set up ignore=34th56 ignore=re45ty ignore=rt45yu . . ignore=rthg34 n is a variable. I have another variable that i read from a different file. It is $2 and it is working the way i expect. array ignore read and print correct values. in the below if... (2 Replies)
Discussion started by: usustarr
2 Replies

8. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

9. Shell Programming and Scripting

Accessing variable from awk program in shell

Hi, I want to access a variable outside the awk program. My program is as below:- I can not access the exact value of k (See the last line of the program). #!/usr/bin/sh j=10 k=1 #k is declared outside awk awk ' BEGIN { i=1; j1="'"$j"'" printf("\n ## Value of j1 is %d ##", j1); ... (2 Replies)
Discussion started by: shouvik.mitra
2 Replies

10. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies
Login or Register to Ask a Question