Linux 如何使用单个命令行在第三个字段中显示最大值

Linux 如何使用单个命令行在第三个字段中显示最大值,linux,bash,unix,Linux,Bash,Unix,我有一个名为file1的文本文件。它包含三列数据。 如何使用单个命令行在第三个字段中显示最大值 111 222 333 555 222 222 444 111 212 222 111 111 它尝试了以下命令: cut -f3 file1 | sort -r 我会使用awk: awk '{m=$3>m?$3:m}END{print m}' file 顺便说一句,关于您尝试的命令行,它应该是: cut -d' ' -f3 file | sort -nr | head -n1

我有一个名为file1的文本文件。它包含三列数据。
如何使用单个命令行在第三个字段中显示最大值

111 222 333
555 222 222
444 111 212
222 111 111
它尝试了以下命令:

cut -f3 file1 | sort -r 

我会使用
awk

awk '{m=$3>m?$3:m}END{print m}' file

顺便说一句,关于您尝试的命令行,它应该是:

cut -d' ' -f3 file | sort -nr | head -n1
       |                         |
       |                         +------------ pipe to head to get just the max
       |
       +---------- cut needs a delimiter

如果目标是按降序打印排序后的值,我会这样做

awk 'NR==FNR{a[$3];next}
      END{asorti(a);
      printf "Sorted values from col3\n";
      while(NR>0){           #NR or the Number of Records comes handy here
      printf "%d\n",a[NR--]  #awk supports post-fix operations using --,++
      }
   }' yourfile
输出:

Sorted values from col3
333
222
212
111

这将有助于:

cut -d ' ' -f3 file1 |sort -n |tail -1

用空格(
)作为分隔符剪切字段3,对它们进行数字排序(
-n
标志),然后取最后一个条目。

您尝试过什么?剪切-f3文件1 |排序-rhehe,我高举您的awk:-)
sort -rk 3 | awk '{print $3;exit}'
awk '$3>a{a=$3}END{print a}'
cut -d ' ' -f3 file1 |sort -n |tail -1