Question

In: Computer Science

Please answer the following using bash shell. 1. The students.txt file consists of rows of students...

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.

Solutions

Expert Solution

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.


Related Solutions

Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Examine the following shell script and describe its function line-by-line: #!/bin/bash echo -e "Which file would...
Examine the following shell script and describe its function line-by-line: #!/bin/bash echo -e "Which file would you like to copy> --> \c" echo -e "Which file would you like to copy? --> \c?" read FILENAME mkdir /stuff || echo "The /stuff directory could not be created." && echo "The /stuff directory could not be created." cp -f $FILENAME /stuff && echo "$FILENAME was successfully copied to /stuff"
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in the current directory whose names end in 't' or 'z'. For such files only, your program should list certain LINES of the file(s). These lines are those that have at least 4 x's somewhere in the line. Note that the x's may be upper- or lower-case, and may be separated by other characters; so the following 3 lines DO match: XXxX and more things...
Please answer the following questions using the data in the attached Excel file. You are thinking...
Please answer the following questions using the data in the attached Excel file. You are thinking of investing in Abercrombie and Fitch Co. (ANF). The returns for ANF are embedded in an Excel document below (Source: yahoo.com). 1.      For the investment in ANF that you are considering, for all of 2012 determine the following items: a) the mean return b) the median return c) the standard deviation d) the variance e) the coefficient of variation The weekly rates of return...
Project 1—UNIX Shell This project consists of designing a C program to serve as a shell...
Project 1—UNIX Shell This project consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. Your implementation will support input and output redirection, as well as pipes as a form of IPC between a pair of commands. Completing this project will involve using the UNIX fork(), exec(), wait(), dup2(), and pipe() system calls and can be completed on any Linux, UNIX, or macOS system. I....
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on...
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on the command line 2. For each file name provided, delete any line that contains the string: qwe.rty When the changes are completed, the script should display the total number of files scanned, and the total number of files changed. Example Command: assessment-script-a file.a file.b file.c file.d file.e    Example Output: 5 files scanned, 3 files changed 3. Your script should include a series of...
Using R Studio: 1)Use the `mtcars` data (`data(mtcars)`) to answer these questions: a) Which rows of...
Using R Studio: 1)Use the `mtcars` data (`data(mtcars)`) to answer these questions: a) Which rows of the data frame contain cars that weigh more than 4000 pounds (the variable is `wt`, units are 1000 pounds). b) Which cars are these? (*Hint:* since rows are named by car name, use `row.names()`). c) What is the mean displacement (in inches^3^ ) for cars with at least 200 horsepower (`hp`). d) Which car has the highest fuel economy (`mpg`)? e) What was the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT