tsukimi
tsukimi

Reputation: 1645

Parsing CSV to print line with highest score using bash shell

I have a CSV like this

Parameter Values,Count,% of Results
" david;[email protected];10300 "," 15 "," 50.0% "
" david;[email protected];12300 "," 15 "," 50.0% "
" davidk;[email protected];32300 "," 15 "," 50.0% "
" joe;[email protected];9200 "," 15 "," 50.0% "
" john;[email protected];1500 "," 15 "," 50.0% "

I want to get the line that has the highest number value, in this case 32300

I already made an attempt at this but it uses several commands

export max=$(awk -F, '{split($1,a,";"); print a[3] }' contestEntryTest.csv | tr -d ' "' | sort -nr | head -n 1) ; grep $max contestEntryTest.csv

How can i do the above in less commands or one command, how would a more experienced bash programmer do the above problem, just for the learning experience. Cheers!

Upvotes: 2

Views: 1130

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185179

A pure awk way to do the trick :

awk -F"[; ]" '($4>v){v=$4}END{print v}' FILE

Upvotes: 1

PasteBT
PasteBT

Reputation: 2198

You can use sort, if the file is demo.csv, then

sort -t ';' -k3 -n demo.cvs|tail -n 1

Upvotes: 2

Related Questions