In: Computer Science
Write an AWK program that takes as input salary_file (Imaginary File). The
salary_file consists around 150 records, each record having three fields: name, salary per hour,
and hours worked -- fields 1, 2, and 3, respectively. Save your AWK program in a file. Name the
file according to our standard convention but with .awk extension, hence name it
As5_yourName_p2.awk. Do not forget to have comments inside of your AWK file.
Your AWK program should have all three segments: BEGIN, main (pattern/action), and END
blocks
1. In the BEGIN segment
1. print your name
2. introduce/initiate variables that you are going to use in parts 2. and 3. of this
Problem
2. In the main block/segment, print
1. only names and salaries of employees that earned more than $700.00
2. names of all employees whose names start with capital A, G or W
3. In the END segment, print
1. total number of employees
2. cumulative hours worked by all employees
3. name of the employee that earned the most
4. name of the file that AWK (this program) is processing
Program:
awk 'BEGIN {
FS=",";
print "Name: John\n" "Filename:" ARGV[1];
#initialize Variables
Earnings = 0
EmployeeCount=0; TotalAmountPaid=0; CumulativeHours=0 ;
MostHoursWorked=0; EmployeeMostHoursWorked="";
MostEarned=0; EmployeeMostEarned=""
}
{
EmployeeCount++
#get Calculation Earnings = HourlyPay * Hours_woeked
Earnings = 0
Earnings = ($2*$3)
#TotalAmountPaid will be the total Earning of all Employees
TotalAmountPaid+=Earnings
#CumulativeHours will be the total Working Hours of all
Employees
CumulativeHours+=$3
#Find the MostHoursWorked Employee
if($3 > MostHoursWorked){
MostHoursWorked=$3
EmployeeMostHoursWorked=$1
}
#Find the MostEarned Employee
if(Earnings > MostEarned){
MostEarned=Earnings
EmployeeMostEarned=$1
}
#print EmployeeName and Earnings of an Employee when Earnings
> 700
if(Earnings > 700 ){
print $1 " " Earnings
}
#print EmployeeName of an Employee when names start with capital
A, D, F, or W and whose names are at least 4 characters long
if(length($1)>=4 && (substr($1,0,1)=="A" ||
substr($1,0,1)=="D" || substr($1,0,1)=="W" ||
substr($1,0,1)=="W")){
print $1
}
}
END{
#print Results Calculated
print "Total EmployeeCount =" EmployeeCount
print "TotalAmountPaid =" TotalAmountPaid
print "CumulativeHours =" CumulativeHours
print "EmployeeMostHoursWorked =" EmployeeMostHoursWorked "
MostHoursWorked = "MostHoursWorked
print "EmployeeMostEarned =" EmployeeMostEarned " MostEarned =
"MostEarned
}
' salary_data_file
-------------------InpputFile(salary_data_file)------------------------
OLIVIA,24,1
RUBY,25,2
EMILY,26,3
GRACE,27,4
JESSICA,28,5
CHLOE,29,6
SOPHIE,30,7
LILY,31,8
AMELIA,32,9
EVIE,33,10
MIA,34,11
ELLA,35,12
CHARLOTTE,36,13
LUCY,37,14
MEGAN,38,15
ELLIE,39,16
ISABELLE,40,17
ISABELLA,41,18
HANNAH,42,19
KATIE,43,20
AVA,44,21
HOLLY,45,22
SUMMER,46,23
MILLIE,47,24
DAISY,48,25
PHOEBE,49,26
FREYA,50,27
ABIGAIL,51,28
POPPY,52,29
ERIN,53,30
EMMA,54,31
MOLLY,55,32
IMOGEN,56,33
AMY,57,34
JASMINE,58,35
ISLA,58,36
SCARLETT,60,37
LEAH,61,38
SOPHIA,62,39
ELIZABETH,63,40
EVA,64,41
BROOKE,65,42
MATILDA,66,43
CAITLIN,67,44
KEIRA,68,45
ALICE,69,46
LOLA,70,47
LILLY,71,48
AMBER,72,49
ISABEL,73,50
LAUREN,74,51
GEORGIA,75,52
GRACIE,76,53
ELEANOR,77,54
BETHANY,78,55
MADISON,79,56
AMELIE,80,57
ISOBEL,81,58
PAIGE,82,58
LACEY,83,60
SIENNA,84,61
LIBBY,85,62
MAISIE,86,63
ANNA,87,64
REBECCA,88,65
ROSIE,89,66
TIA,90,67
LAYLA,73,68
MAYA,74,69
NIAMH,75,70
ZARA,76,71
SARAH,77,72
LEXI,78,73
MADDISON,79,74
ALISHA,80,75
SOFIA,81,76
SKYE,82,77
NICOLE,83,78
LEXIE,84,79
FAITH,85,80
MARTHA,86,81
HARRIET,87,82
ZOE,6,83
EVE,7,84
JULIA,8,85
AIMEE,9,86
HOLLIE,10,87
LYDIA,11,88
EVELYN,12,89
ALEXANDRA,13,90
MARIA,14,91
FRANCESCA,15,92
TILLY,16,93
FLORENCE,17,94
ALICIA,18,95
ABBIE,19,96
EMILIA,20,97
COURTNEY,21,98
MARYAM,22,99
ESME,20,83
--------------------------output----------------------
Name: John
Filename:salary_data_file
AMELIA
LUCY 518
MEGAN 570
ELLIE 624
ISABELLE 680
ISABELLA 738
HANNAH 798
KATIE 860
AVA 924
HOLLY 990
SUMMER 1058
MILLIE 1128
DAISY 1200
DAISY
PHOEBE 1274
FREYA 1350
ABIGAIL 1428
ABIGAIL
POPPY 1508
ERIN 1590
EMMA 1674
MOLLY 1760
IMOGEN 1848
AMY 1938
JASMINE 2030
ISLA 2088
SCARLETT 2220
LEAH 2318
SOPHIA 2418
ELIZABETH 2520
EVA 2624
BROOKE 2730
MATILDA 2838
CAITLIN 2948
KEIRA 3060
ALICE 3174
ALICE
LOLA 3290
LILLY 3408
AMBER 3528
AMBER
ISABEL 3650
LAUREN 3774
GEORGIA 3900
GRACIE 4028
ELEANOR 4158
BETHANY 4290
MADISON 4424
AMELIE 4560
AMELIE
ISOBEL 4698
PAIGE 4756
LACEY 4980
SIENNA 5124
LIBBY 5270
MAISIE 5418
ANNA 5568
ANNA
REBECCA 5720
ROSIE 5874
TIA 6030
LAYLA 4964
MAYA 5106
NIAMH 5250
ZARA 5396
SARAH 5544
LEXI 5694
MADDISON 5846
ALISHA 6000
ALISHA
SOFIA 6156
SKYE 6314
NICOLE 6474
LEXIE 6636
FAITH 6800
MARTHA 6966
HARRIET 7134
EVE 588
JULIA 680
AIMEE 774
AIMEE
HOLLIE 870
LYDIA 968
EVELYN 1068
ALEXANDRA 1170
ALEXANDRA
MARIA 1274
FRANCESCA 1380
TILLY 1488
FLORENCE 1598
ALICIA 1710
ALICIA
ABBIE 1824
ABBIE
EMILIA 1940
COURTNEY 2058
MARYAM 2178
ESME 1660
Total EmployeeCount =101
TotalAmountPaid =268792
CumulativeHours =5032
EmployeeMostHoursWorked =MARYAM MostHoursWorked = 99
EmployeeMostEarned =HARRIET MostEarned = 7134