bash 根据文件的第二列对数据进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6438896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:40:54  来源:igfitidea点击:

Sorting data based on second column of a file

bashshellunix

提问by Angelo

I have a file of two columns and nnumber of rows.

我有一个两列和n行数的文件。

column 1 contains namesand column2 age.

第 1 列包含names和第 2列age

I want to sort the content of this file in ascending order based on the age(in second column).

我想根据age(在第二列中)按升序对此文件的内容进行排序。

The result should display the nameof the youngest person along with nameand then second youngest person and so on...

结果应该显示name最年轻的人以及name第二年轻的人,依此类推......

Any suggestions for a one liner shell or bash script.

对单行 shell 或 bash 脚本的任何建议。

回答by Matt Ryall

You can use the sortcommand:

您可以使用以下sort命令

sort -k2 -n yourfile

-n, --numeric-sortcompare according to string numerical valu

-n,--numeric-sort根据字符串数值比较

For example:

例如:

$ cat ages.txt 
Bob 12
Jane 48
Mark 3
Tashi 54

$ sort -k2 -n ages.txt 
Mark 3
Bob 12
Jane 48
Tashi 54

回答by DCurro

Solution:

解决方案:

sort -k 2 -n filename

sort -k 2 -n filename

more verbosely written as:

更详细地写为:

sort --key 2 --numeric-sort filename

sort --key 2 --numeric-sort filename



Example:

例子:

$ cat filename
A 12
B 48
C 3

$ sort --key 2 --numeric-sort filename 
C 3
A 12
B 48


Explanation:

解释:

  • -k #- this argument specifies the first column that will be used to sort. (note that column here is defined as a whitespace delimited field; the argument -k5will sort starting with the fifth fieldin each line, not the fifth characterin each line)

  • -n- this option specifies a "numeric sort" meaning that column should be interpreted as a row of numbers, instead of text.

  • -k #- 此参数指定将用于排序的第一列。(请注意,此处的列被定义为以空格分隔的字段;参数-k5将从每行中的第五个字段开始排序,而不是每行中的第五个字符

  • -n- 此选项指定“数字排序”,这意味着应将列解释为一行数字,而不是文本。



More:

更多的:

Other common options include:

其他常见选项包括:

  • -r- this option reverses the sorting order. It can also be written as --reverse.
  • -i- This option ignores non-printable characters. It can also be written as --ignore-nonprinting.
  • -b- This option ignores leading blank spaces, which is handy as white spaces are used to determine the number of rows. It can also be written as --ignore-leading-blanks.
  • -f- This option ignores letter case. "A"=="a". It can also be written as --ignore-case.
  • -t [new separator]- This option makes the preprocessing use a operator other than space. It can also be written as --field-separator.
  • -r- 此选项颠倒排序顺序。它也可以写成--reverse
  • -i- 此选项忽略不可打印的字符。它也可以写成--ignore-nonprinting
  • -b- 此选项忽略前导空格,这很方便,因为空格用于确定行数。它也可以写为--ignore-leading-blanks
  • -f- 此选项忽略字母大小写。“一个”==“一个”。它也可以写成--ignore-case
  • -t [新分隔符]- 此选项使预处理使用除空格以外的运算符。它也可以写成--field-separator

There are other options, but these are the most common and helpful ones, that I use often.

还有其他选项,但这些是我经常使用的最常见和最有用的选项。

回答by Saurabh

For tab separated values the code below can be used

对于制表符分隔值,可以使用以下代码

sort -t$'\t' -k2 -n

-r can be used for getting data in descending order.
-n for numerical sort
-k, --key=POS1[,POS2] where k is column in file
For descending order below is the code

-r 可用于按降序获取数据。
-n 用于数字排序
-k, --key=POS1[,POS2] 其中 k 是文件中的列
下面的降序是代码

sort -t$'\t' -k2 -rn

回答by Ignacio Vazquez-Abrams

Use sort.

使用sort.

sort ... -k 2,2 ...