LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2020, 08:19 PM   #1
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Rep: Reputation: Disabled
Exclamation 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
 
Old 04-01-2020, 08:31 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,473

Rep: Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453
Quote:
Originally Posted by jessmister04 View Post
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.
 
Old 04-01-2020, 09:57 PM   #3
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Original Poster
Rep: Reputation: Disabled
Smile 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
 
Old 04-01-2020, 10:07 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,473

Rep: Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453
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 ...
 
Old 04-04-2020, 01:36 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,621

Rep: Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732
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).

Last edited by MadeInGermany; 04-04-2020 at 01:43 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
please help understand routing xrado Linux - Networking 3 04-09-2006 06:44 PM
Please help me understand aptitude coralsaw Debian 9 01-13-2006 09:54 AM
Please help me understand the SuSE 9.3 Pro install process Jongi SUSE / openSUSE 6 09-09-2005 09:43 AM
Please help me understand my dmesg print out. CyberLord_7 Mandriva 2 03-11-2004 12:30 PM
Please help me understand DNS doughammond Linux - Networking 4 10-16-2003 10:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration