In: Computer Science
Please answer the following using bash shell.
1. The students.txt file consists of rows of students where each row contains a student’s first name, last name, major, and 5 test scores. Write a script that uses a while read statement to input all of the students, line by line, computes the average test score and determines the letter grade for that student. Of the 5 test scores, the 5th test is worth double so that you add each test score to a sum but add the 5th test score twice. The average divides by 6 instead of 5. Output for each student, the student’s last name, test average and letter grade. Additionally, sum up the number of students who passed (got at least a D or higher) and the number of students who failed. Output these results at the end. When you run your script, remember to redirect input from students.txt.
2. Write a script that inputs all of the student information and counts the number of students whose major matches the parameter passed to the script. For instance, you might call this script as ./script3 CIT < students.txt, which then counts the number of students whose major is CIT. Output each student by name and the number of students who matched.
1.) The following is the bash script for 1st question
awk 'BEGIN {
FS=" ";
}
{
fname[NR]=$1;
lname[NR]=$2;
average[NR]=($4+$5+$6+$7+$8+$8) / 6;
total += average[NR];
if(average[NR]>90 && average[NR]<=100)
{
grade[NR]="A";
}
else if(average[NR]>80 && average[NR]<=90)
{
grade[NR]="B";
}
else if(average[NR]>70 && average[NR]<=80)
{
grade[NR]="C";
}
else if(average[NR]>60 && average[NR]<=70)
{
grade[NR]="D";
}
else if(average[NR]>50 && average[NR]<=60)
{
grade[NR]="E";
}
else
{
grade[NR]="F";
}
}
# End Block
END {
print "Last Name\tAverage\t\tGrade\n";
i = 1;passed=0;failed=0;
while (i <= FNR) {
printf("%-10s\t %.2f\t\t%-10s\n", lname[i] , average[i],grade[i]);
if(grade[i]<="D")
{
passed+=1;
}
else
{
failed+=1;
}
i++;
}
printf("Total number of students passed : %d\n",passed);
printf("Total number of students failed : %d\n",failed);
}'
Note: Since it is not mentioned in the question the limit of grades, I have mentioned the grades till F. If you want to keep the grades till E, you can remove that condition. In short, NR represents total no.of lines(total records) passed till now(current line) and FNR represents record number. All the requirements are met in the above code. Please feel free to ask doubts if any, in the comments section.
Input: I considered the following as input.txt since the input file is not attached with your question.
John Paul CSE 80 75 67 88 91
Colin Munro Mech 78 64 55 54 66
Prakhar Saxena Mech 91 82 82 93 85
Mark Brew BioTech 99 94 95 78 89
Output: I am also attaching the output for your reference.
2.) The following is the bash script for 2nd question
awk 'BEGIN {
FS=" ";
}
{
fname[NR]=$1;
lname[NR]=$2;
major[NR]=$3;
average[NR] = ($4+$5+$6+$7+$8+$8) / 6;
}
# End Block
END {
total=0;
major_="CSE";
printf("Students matching with major %s\n",major_);
while (i <= FNR) {
if(major[i]=="CSE")
{
printf("%s %s\n",fname[i],lname[i]);
total+=1;
}
i++;
}
printf("Total number of students matched with %s : %d\n",major_,total);
}'
Note: Please initialize the major_variable with command line input argument. Since i cannot execute the command here i have directly initialized the variable. Hope you understand.
Input: I have taken the same input as above
John Paul CSE 80 75 67 88 91
Colin Munro Mech 78 64 55 54 66
Prakhar Saxena Mech 91 82 82 93 85
Mark Brew BioTech 99 94 95 78 89
Output: I am attaching the output for your reference.
#Please provide positive rating and upvote if you like my work.This will help me to provide better solutions with great efforts. Thank you.