Bash输出具有最高值的行

teu*_*ara 5 linux sorting bash uniq

我的问题非常像这个问题,但有一点不同; 我希望输出在第3个选项卡上得分最高的行.我的数据如下:

1.gui  Qxx  16
2.gui  Qxy  23
3.guT  QWS  11
Run Code Online (Sandbox Code Playgroud)

我想得到这个:

1.gui  Qxy  23
3.guT  QWS  11
Run Code Online (Sandbox Code Playgroud)

我用了:

cat file.f | uniq | cut -d" " -f3 | sort | uniq -d >>out.f
Run Code Online (Sandbox Code Playgroud)

但没有得到我想要的东西!?

Chr*_*our 8

sort:

$ sort -rk3 file             # Sort on column 3, display all results

2.gui  Qxy  23
1.gui  Qxx  16
3.guT  QWS  11

$ sort -rk3 file | head -2   # Sort on column 3, filter number of results

2.gui  Qxy  23
1.gui  Qxx  16

$ sort -rk3 file | uniq      # Sort on column 3, on display unique results 

2.gui  Qxy  23
1.gui  Qxx  16
3.guT  QWS  11
Run Code Online (Sandbox Code Playgroud)

-r 反向排序,最高的.

-k3 排在第3列.


如果您只想显示第3列大于某个值(即15)的行,请尝试使用awk:

awk '$3>15' file | sort -rk3  # Display line where column 3 > 15 and sort

2.gui  Qxy  23
1.gui  Qxx  16
Run Code Online (Sandbox Code Playgroud)


teu*_*ara 5

对于有同样问题的未来用户:

不要忘记在命令中引入 -n 开关-sort,或者您的值从 9999 开始排序,然后是 999 等。所以使用

sort -rnk3 file
Run Code Online (Sandbox Code Playgroud)

如果您只想获取具有最高值的一行(删除重复项),请使用以下命令:

sort -rnk3 file | awk '!x[$2]++'
Run Code Online (Sandbox Code Playgroud)

如果你有一个常用的分隔符,你可以告诉-awk注意:

sort -rnk3 file | awk -F"[. ]" '!x[$2]++'
Run Code Online (Sandbox Code Playgroud)