152k views
2 votes
Consider the file USA.txt, which has millions of lines like this:

Shellstrop Eleanor Phoenix AZ 85023 -2920765
Shellstrop Donna Tarantula_Springs NV 89047 -5920765
Mendoza Jason Jacksonville FL 32205 -4123794
Mendoza Douglas Jacksonville FL 32209 -3193274 (Donkey Doug)
Peleaz Steven Jacksonville FL 32203 -3123794 (Pillboi)
. . .
Each line has last name, first name, city, state, zip, and points (which may be negative, positive,
or zero). Some lines may have extra information, such as nicknames.
We want the line for the person not from Florida with the highest point total.
(To be clear, −10 < −5 < 0 < 4.)
(a) Give a command that uses only awk to find that value.

(b) Give a command that uses anything but awk (grep, sed, a pipeline of both or more,
whatever) to make that list.

User Uni Le
by
7.5k points

1 Answer

5 votes

Final answer:

To find the person not from Florida with the highest point total, you can use awk or a pipeline of grep and sed commands.

Step-by-step explanation:

To find the person not from Florida with the highest point total using awk, you can use the following command:

awk '{if ($4 != "FL" && $6 > max) {max = $6; line = $0}} END {print line}' USA.txt

This command checks if the state ($4) is not equal to "FL" and if the points ($6) are greater than the current maximum. If so, it updates the maximum and stores the entire line. Finally, it prints the line with the highest point total.

To achieve the same result using other commands like grep and sed, you can use the following pipeline:

grep -v "FL" USA.txt | sort -k6nr | head -1

This command first filters out the lines from Florida using grep, then sorts the remaining lines based on the points field in reverse numeric order using sort, and finally selects the line with the highest point total using head.

User Shreesha N
by
7.5k points