LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Please help me understand this. (https://www.linuxquestions.org/questions/linux-newbie-8/please-help-me-understand-this-4175672379/)

jessmister04 04-01-2020 07:19 PM

Please help me understand this.
 
Hello, I am in my second UNIX class and am having a little bit of problems. This is our first week covering awk commands. We are using a csv file called chores.csv. We also are writing the awk commands in a separate file. Below is my code for average.awk. My goal is to turn average.awk into a file that will take in chores.csv information and output the average length of time the chores would take to complete. My code works good except the average is wrong because the header is included in N. The correct output should read 32.14. Thank you for any tips or help.

average.awk #code executed before input fields

BEGIN{
FS="," #field separator set to comma
}
{
if(NR!=1) #if Number of Rows is not = 1

sum += 3 #sum will add and assign to itself field $3

}
END{
avg=sum/NR #Here is my problem I think NR is adding
# the header right??

printf("Average: %.2f\n", avg) #print Average floating-point num
#with two decimal places.





Here is the chores.csv

Chore Name,Assigned to,estimate,done?
Laundry,Chelsey,45,N
Wash Windows,Sam,60,Y
Mop kitchen,Sam,20,N
Clean cookware,Chelsey,30,N
Unload dishwasher,Chelsey,10,N
Dust living room,Chelsey,20,N
Wash the dog,Sam,40,N

syg00 04-01-2020 07:31 PM

Quote:

Originally Posted by jessmister04 (Post 6106811)
avg=sum/NR #Here is my problem I think NR is adding
# the header right??

Correct. If it's one too big, what's the simple answer to fix that ?. Don't worry, you won't affect the actual value of NR.

jessmister04 04-01-2020 08:57 PM

Hi syg00
 
Hi syg00, Well, I tried many things to subtract the 1 from NR but my syntax must be off because I am struggling. I tried the following: Actually during this post I figured it out and it works perfect. Thanks for the tip or thought syg00, hope to pick your brain in the future if I have any further questions. Went from 6 weeks of python, to 6 weeks of java, to unix, to now shell scripting, so I need to definitely get the syntax down for each and problem solve better.

avg=sum/NR-1 output= 7, which is what I kind of need. This takes away the header and shows the correct 7 fields I feel

Here us what worked for me.
avg=sum/(NR-1)- Actually I think this syntax did it. Geez, now I feel kind of dumb. I knew what to do but

syg00 04-01-2020 09:07 PM

Good for you.
Different operations have different precedence - to make sure they take place in the order you want (the subtraction before the division) use brackets just as you did.

It's all easy from now on ... :p

MadeInGermany 04-04-2020 12:36 AM

A general alternative - overkill here - is to introduce a second variable
Code:

...
{
  if(NR != 1) {
    sum+=$3
    nr++
  }
}
END{
  avg=sum/nr
...

You can initialize nr=0 in the BEGIN section, for clarity. But in awk a not initialized variable is 0 in number context (and "" in string context).


All times are GMT -5. The time now is 05:22 PM.