Reputation: 459
I have txt files containing lines like these:
22 ./06a_kéz.model.txt
22 ./08a_ív.model.txt
23 ./12_iker.model.txt
23 ./15_szék.model.txt
14 ./06_ta.model.txt
1 ./03a_ösc.model.txt
I need to select the lines with higher scores, I attempt this:
awk '{ if ($1 > score) score=$1" "$2}END{print score}'
But I need to extract in this given example, the two lines with the highest score.
expected output:
23 ./12_iker.model.txt
23 ./15_szék.model.txt
Upvotes: 1
Views: 45
Reputation: 20002
Without awk (assuming your scores are in a file called file with a space between score and the rest of the line):
grep "^$(cut -d " " -f1 file | sort -rn | head -1) " file
Upvotes: 0
Reputation: 9570
I have no awk
here to test, but would try something like this script:
{ if ($1 == score) { result = result $0 "\n"}
if ($1 > score) { score=$1; result = $0 "\n"}}
END {print result}
As a one-liner:
awk '{ if ($1 == score) { result = result $0 "\n"} if ($1 > score) { score=$1; result = $0 "\n"}} END{print result}'
Upvotes: 0
Reputation: 785226
Using this standard 2 pass pattern in awk:
awk 'FNR==NR{if ($1>max) max=$1; next} $1==max' file file
23 ./12_iker.model.txt
23 ./15_szék.model.txt
Alternatives you can use this little longer single pass awk command:
awk '$1>=max{max=$1; a[max] = (a[max]!="")?a[max] ORS $0: $0} END{print a[max]}' file
23 ./12_iker.model.txt
23 ./15_szék.model.txt
Upvotes: 1