3

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.

  • 1
    do not pass the file name `grades` to `awk`. Let `awk` feed off the pipe – iruvar Nov 20 '13 at 03:24
  • Great, thanks. Do you know by chance how those two numbers could be used so that I can print Lowest quiz is ? I'm not sure what they can be called so I can use them –  Nov 20 '13 at 03:29

2 Answers2

0

you can use head, and tail

head will get first

tail will get the last

user1050632
  • 680
  • 4
  • 13
  • 26
0
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.

user unknown
  • 35,537
  • 11
  • 75
  • 121
  • Is there a way to use this code and add the grades file as a parameter to it. So if I go to run it I could use ./minMax grades and it would work? –  Nov 20 '13 at 03:39
  • Got it, I replace grades with $1 and then it can be passed into it. Thanks again for all your help! –  Nov 20 '13 at 03:58