从Bash中的某些列中提取最大值

时间:2015-09-09 06:37:09

标签: bash

请告诉我如何编写一个找到以下内容的bash脚本:

  1. 包含第1列

  2. 中5个最大值的行
  3. 包含第1列

  4. 中第二大值的行
  5. 包含第1列

  6. 中5个最大值的行的行号

1 个答案:

答案 0 :(得分:1)

您可以将sortheadtail

一起使用
# the lines containing the 5 largest values from column 1
sort -n somefile.csv --reverse | head -n 5
# the line containing the second largest value from column 1
sort -n somefile.csv --reverse | head -n 2 | tail -n 1

对于最后一部分,您还需要行号,因此请使用cat --number添加它们并使用相同的方法:

cat --number somefile.csv | sort -nk2 --reverse | head -n 5

请注意,第2列用作最后一个命令中的键,因为第一列包含行号。