I want to find the average rainfall in three states, for example, CA, TX and AX for a particular month from January to December. This input file is limited TAB SPACESand has a format
city name, the state , and then average rainfall amounts from January through December, and then an annual average for all months. EG may look like
AVOCA PA 30 2.10 2.15 2.55 2.97 3.65 3.98 3.79 3.32 3.31 2.79 3.06 2.51 36.18
BAKERSFIELD CA 30 0.86 1.06 1.04 0.57 0.20 0.10 0.01 0.09 0.17 0.29 0.70 0.63 5.72
What I want to do is "Get the sum of the average rainfall for a given month feb, for more than n years, and then find the average value for the states CA, TX and AX.
I wrote below script in awk to do the same, but it does not give me the expected output
/^CA$/ {CA++; CA_SUM+= $5}
/^TX$/ {TX++; TX_SUM+= $5}
/^AX$/ {AX++; AX_SUM+= $5}
END {
CA_avg = CA_SUM/CA;
TX_avg = TX_SUM/TX;
AX_avg = AX_SUM/AX;
printf("CA Rainfall: %5.2f",CA_avg);
printf("CA Rainfall: %5.2f",TX_avg);
printf("CA Rainfall: %5.2f",AX_avg);
}
I call the program using the command
awk 'FS="\t"'-f awk1.awk rainfall.txtand do not see the exit.
Question: Where am I slipping? Any suggestions and modified code will be appreciated
source
share