Question

This is a homework assignment and I'm a bit stumped here. The objective is as follows:

Create a file called grades that will contain quiz scores. The file should be created so that there is only one quiz score per line. Write a script called minMax that will accept a parameter that represents the file grades and then determine the minimum and maximum scores received on the quizzes. Your script should display the output in the following format: Your highest quiz score is #. Your lowest quiz score is #.

What I have done to accomplish this is first sort the grades so that it goes in order. Then I attempted to pipe it with this command such as this:

sort grades |awk 'NR==1;END{print}' grades

The output I get when I am done is the first and last entry of the file, but its no longer sorted and I'm not sure how to pick out the first and last to print them, is it $1 and $2?

Any help would be greatly appreciated.

Was it helpful?

Solution

sort -n grades | sed -n '1s/.*/Lowest: &/p;$s/.*/Highest: &/p;' 
Lowest: 2
Highest: 19

You need to sort -n if you want to sort by number. With sed, you may handle it in one pass.

Multiple Sed comamnds are concatenated by ;. 1s and $s mean the first and last line. & is the whole read expression/line. p prints the result. -n is -no printing in general.

OTHER TIPS

you can use head, and tail

head will get first

tail will get the last

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top