I'm very new at Bash Scripting so forgive my ignorance. I have a text file with last names that I am reading and processing line-by-line an average of three test scores. I need to output the lines sorted by last name. Here is what I have to compute the average, with a variable named OUTTOSORT that I was trying to pass to sort before being output.
#!/bin/bash FILE=$1 OUTTOSORT="" while read -r SID FIRST LAST S1 S2 S3 do SUM=$(expr $S1 + $S2 + $S3) AVG=$(expr $SUM / 3) OUTTOSORT = OUTTOSORT + "$AVG [$SID] $LAST, $FIRST \n" done < "$FILE" sort < $OUTTOSORT
engr2-6-52-dhcp:homework6 Matt$ ./Grades.sh ./Grades.sh: line 10: : No such file or directory ./Grades.sh: line 12: $OUTTOSORT: ambiguous redirect
Any suggestions on how to sort these lines after computing the average would be very helpful. Thanks!
Thanks very much for your answers! I now have a problem where it's not sorting by the field I specify however:
#!/bin/bash FILE=$1 COUNT=0 while read -r SID FIRST LAST S1 S2 S3 do SUM=$(expr $S1 + $S2 + $S3) AVG=$(expr $SUM / 3) printf '%d [%d] %s, %s (%d)\n' "$AVG" "$SID" "$LAST" "$FIRST" "$COUNT" COUNT=$((COUNT + 1)) done < "$FILE" | sort -k 3,3n -k 4,4n -k 2,2g
71  Camp, Laney (5) 80  Camp, Skyler (6) 81  Camp, Tracey (4) 82  Johnson, Lee (0) 82  Forester, Jess (7) 91  Smith, Jaime (1) 92  Forney, JC (2) 93  Forester, Chris (8) 99  Lee, Terry (3)
As you can see, it is sorting by average score instead of by Last name, First name, SID. I added a counter to confirm it was sorting across the lines of the output. What error is causing the sort to not use the correct keys? Thanks!
Solution to sort problem found: type flags for last name and first name should be d instead of n, making the sort command the while loop command pipes to
sort -k 3,3d -k 4,4d -k 2,2g.