2016-04-01 30 views
1

我有一個包含這樣的詩句TXT文件:AWK選擇更高的分數

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 

我需要選擇具有較高的分數線,我嘗試這樣的:

awk '{ if ($1 > score) score=$1" "$2}END{print score}' 

但我需要提取這個例子,得分最高的兩條線。

預期輸出:

23 ./12_iker.model.txt 
23 ./15_szék.model.txt 

回答

1

使用AWK這個標準2次通過模式:

awk 'FNR==NR{if ($1>max) max=$1; next} $1==max' file file 

23 ./12_iker.model.txt 
23 ./15_szék.model.txt 

替代品,你可以使用這個小長單次awk命令:

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 
0

我有沒有awk在這裏測試,但會嘗試像這樣的腳本:

{ if ($1 == score) { result = result $0 "\n"} 
     if ($1 > score) { score=$1; result = $0 "\n"}} 
END {print result} 

作爲一個班輪:

awk '{ if ($1 == score) { result = result $0 "\n"} if ($1 > score) { score=$1; result = $0 "\n"}} END{print result}' 
0

沒有AWK(假設你的分數與分數和行的其餘部分之間的空間一個名爲文件):

grep "^$(cut -d " " -f1 file | sort -rn | head -1) " file