Calculating Total and Averages with awk Commands & Scripts

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Calculating Total and Averages with awk Commands & Scripts
# 1  
Old 03-31-2014
Question Calculating Total and Averages with awk Commands & Scripts

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Write an awk script(company.awk) for the workers file to find the number of workers of each departman, total salary that is the sum of the salaries of each departmant workers, total salary of all departments and total salary average.(Total average should be calculated by dividing total salary to the total number of workers)

2. Relevant commands, code, scripts, algorithms:
Workers file;
Code:
Alisan Baltaci, 23, Engineer, CUS, 1, 1800
Kadir Yenigun, 25, Technician, CIS, 2, 1000
Hande Gunay, 21, Secretary, 1, 850
Baran Tiryaki, 29, Engineer, SRT, 4, 2500
Bahar Topak, 26, Engineer, CIS, 3, 2000
Cihan Kurt, 28, Technician, SRT, 5, 1250
Sezen Agaoglu, 25, Engineer, CUS, 3, 2000
Kaan Yuksel, 21, Technician, CUS, 2, 1000
Irem Sozeri, 23, Engineer, CUS, 1, 1800
Dilan Colpan, 24, Secretary, CIS, 4, 1000

Output should be like this;
Code:
DEPARTMENTS & SALARIES
Department Workers TotalSalary Average
--------------------------------------------------------------
CUS 4 6600 1650
CIS 3 4000 1333.3
SRT 3 4600 1533.3
--------------------------------------------------------------
GRAND TOTAL 10 15200 1520

Only it should be used awk commands & scripts.

3. The attempts at a solution (include all code and scripts):
In awk scripts;
Code:
BEGIN{print"DEPARTMENTS & SALARIES"
print"Department Workers TotalSalary Average"
print"---------------------------------------------------------"}
{FS=","
GFS="\t"}

{if(/CUS/)
{cus_no++;
cus_tot+=$6;}
else if(/CIS/)
{cis_no++;
cis_tot+=$6;}
else if(/SRT/)
{srt_no++;
srt_tot+=$6;}
}

END{tot_workers=cus_no+cis_tot+srt_no;
tot_salary=cus_tot+cis_tot+srt_tot;
print"CUS",cus_no,cus_tot,cus_tot/cus_no
print"CIS",cis_no,cis_tot,cis_tot/cis_no
print"SRT",srt_no,srt_tot,srt_tot/srt_no
print"------------------------------------------------"
print"GRAND TOTAL",tot_workers,tot_salary,tot_salary/tot_workers}

When apply this script, the codes do not count first latter and output seems 1801 in first calculation. However, when I type a blank for firs line and I start to type workers file in second line, there is no problem.

The question is why. Why does script count first line when it is typed to first line in workers file?

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Bilkent University, Ankara(Çankaya), Turkey, Engin Zafer Kıraçbedel, and CTE 218 (cte.bilkent.edu.tr/en/curriculum)

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by bartus11; 03-31-2014 at 03:32 PM.. Reason: Please use [code][/code] tags.
# 2  
Old 03-31-2014
1. Your input file apparently misses one SRT, , should be
Code:
Alisan Baltaci, 23, Engineer, CUS, 1, 1800
Kadir Yenigun, 25, Technician, CIS, 2, 1000
Hande Gunay, 21, Secretary, SRT, 1, 850
Baran Tiryaki, 29, Engineer, SRT, 4, 2500
Bahar Topak, 26, Engineer, CIS, 3, 2000
Cihan Kurt, 28, Technician, SRT, 5, 1250
Sezen Agaoglu, 25, Engineer, CUS, 3, 2000
Kaan Yuksel, 21, Technician, CUS, 2, 1000
Irem Sozeri, 23, Engineer, CUS, 1, 1800
Dilan Colpan, 24, Secretary, CIS, 4, 1000

2. FS sets the field separator for the following input lines.
Look where you have defined it!
# 3  
Old 03-31-2014
What is GFS? Did you mean OFS?
# 4  
Old 03-31-2014
Some critiques:

Try putting spaces between your print commands (i.e. print "GRAND TOTAL") and quotes.
The third line in the workers file is missing a department.
The calculations are off using the $6 variable. Since the salary is at the end of the line,
try replacing it with $NF instead.
You have a typo in this line:
END{tot_workers=cus_no+cis_tot+srt_no;

I re-wrote your script and here's my output:

Code:
./company.awk workers.txt
DEPARTMENTS & SALARIES
Department Workers TotalSalary Average
----------------------------------------
CUS             4       6600    1650
CIS             3       4000    1333.33
SRT             3       4600    1533.33
----------------------------------------
GRAND TOTAL     10      15200   1520

Hope this helps.
This User Gave Thanks to in2nix4life For This Post:
# 5  
Old 03-31-2014
Quote:
Originally Posted by Corona688
What is GFS? Did you mean OFS?
Yes, sorry about miswriting.

Quote:
Originally Posted by in2nix4life
Some critiques:

Try putting spaces between your print commands (i.e. print "GRAND TOTAL") and quotes.
The third line in the workers file is missing a department.
The calculations are off using the $6 variable. Since the salary is at the end of the line,
try replacing it with $NF instead.
You have a typo in this line:
END{tot_workers=cus_no+cis_tot+srt_no;

I re-wrote your script and here's my output:

Code:
./company.awk workers.txt
DEPARTMENTS & SALARIES
Department Workers TotalSalary Average
----------------------------------------
CUS             4       6600    1650
CIS             3       4000    1333.33
SRT             3       4600    1533.33
----------------------------------------
GRAND TOTAL     10      15200   1520

Hope this helps.
Thanks mate, I tried your way, and there is no problem. However, I do not understand. What is difference between $6 and $NF?

Last edited by RedJohn; 03-31-2014 at 05:31 PM.. Reason: Miswriting
# 6  
Old 03-31-2014
Quote:
Originally Posted by RedJohn
... ... ...

Thanks mate, I tried your way, and there is no problem. However, I do not understand. What is difference between $6 and $NF?
When you had this mistaken input line:
Code:
Hande Gunay, 21, Secretary, 1, 850

using $NF gives you the salary from this line (even though a field was missing) while $6 gives you 0. But, you might be better off printing an error if you find that an input line doesn't have the correct number of fields. For example, if you had included the line:
Code:
NF != 6 {printf("Line %d has %d fields; expected 6.\n", NR, NF)}

It would have told you that line 1 had 7 fields (since the fields were set before FS had been set) and that line 3 had 6 fields (and you would have looked at that line again and quickly seen that the Department was missing).
You could also consider adding a final else clause to your if statement with something like:
Code:
else print "Unrecognized department on line " NR

Tests like these may not be needed if your input is generated by verified software; but if your input is generated by humans, assume they will make mistakes that will cause your code to fail in unexpected ways.
# 7  
Old 03-31-2014
Quote:
Originally Posted by Don Cragun
...assume they will make mistakes that will cause your code to fail in unexpected ways.
Thanks a lot. Now it makes sense. I wrote workers file correctly, not included missing field part but script still calculates wrong.The firs calculation was 1801. However, with $NF, there is no problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculating Total Hours worked

Write a script using a Linux shell programming language to perform clock management for a small daycare. The program should manage all time in and out routines. At the end of the each day should give the Total hours worked that day. Example: Time-In 6:30am Lunch-Out 11 :25am... (1 Reply)
Discussion started by: sarapham409
1 Replies

2. What is on Your Mind?

OSX 10.14 Mojave Commands - 13K+ Total Man Pages in Repository

Just added OSX 10.14 Mojave Commands (currently over 13K pages in the mojave repo) to our man page repository: OSX 10.14 Mojave Commands We need to update all the man pages to the most current versions, so please contribute man page sets to your favorite OS environment (tar.gz with os and... (3 Replies)
Discussion started by: Neo
3 Replies

3. Shell Programming and Scripting

Awk- Pivot Table Averages

Hi everyone, Has anyone figured out yet how to do pivot table averages using AWK. I didn't see anything with regards to doing averages. For example, suppose you have the following table with various individuals and their scores in round1 and round2: SAMPLE SCORE1 SCORE2 British ... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

4. Shell Programming and Scripting

Performance of calculating total number of matching records in multiple files

Hello Friends, I've been trying to calculate total number of a certain match in multiple data records files (DRs). Let say I have a daily created folders for each day since the beginning of july like the following drwxrwxrwx 2 mmsuper med 65536 Jul 1 23:59 20150701 drwxrwxrwx 2 mmsuper... (1 Reply)
Discussion started by: EAGL€
1 Replies

5. Shell Programming and Scripting

Calculating average with awk

I need to find the average from a file like: data => BW:123 M:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1... (4 Replies)
Discussion started by: Slagle
4 Replies

6. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

7. Emergency UNIX and Linux Support

Calculating total space in GB for all files with typical pattern

Hi Experts, In a particular dir, I have many files *AJAY*. How can I get total size of all such files. I tried du -hs *AJAY* but it gave me individual size of all files. All I require is summation of all. Thanks, Ajay (4 Replies)
Discussion started by: ajaypatil_am
4 Replies

8. UNIX for Advanced & Expert Users

Sun: High kernel usage & very high load averages

Hi, I am seeing very high kernel usage and very high load averages on my system (Although we are not loading much data to our database). Here is the output of top...does anyone know what i should be looking at? Thanks, Lorraine last pid: 13144; load averages: 22.32, 19.81, 16.78 ... (4 Replies)
Discussion started by: lorrainenineill
4 Replies

9. Shell Programming and Scripting

Scripts for calculating size and remaining space of a directory automatically.

I would like to create a script for calculating size and remaining space of a directory automatically every 24 hours, then send an email to report to the admin. * POSIX and PERL are preferred. Can anyone help, please? (1 Reply)
Discussion started by: leonall
1 Replies
Login or Register to Ask a Question