linux - 如何按第三列中的数字对数据进行排序?

标签 linux shell unix text

如果我有一个由如下所示的数据组成的文件,我将如何根据第三列中的数字对数据进行排序?

前两列之间的空格不是制表符分隔的,而是一些空格。第二列和第三列之间的间距根据数字的大小而变化。

另请注意,第二列的某些数据中有空格(例如 (p 之间的 lp25( Plasma)),而其他数据则有空格没有任何空格(如染色体)。

HELIX       lp25(plasmid           24437 bp    RNA     linear       29-AUG-2011
HELIX       cp9(plasmid             9586 bp    DNA     helix       29-AUG-2011
HELIX       lp28-1(plasmid         25455 bp    DNA     linear       29-AUG-2011
HELIX       chromosome            911724 bp    DNA     plasmid       29-AUG-2011

最佳答案

给你:

sort -n -k 3 test.txt

来自man sort :

-n, --numeric-sort          compare according to string numerical value
-k, --key=KEYDEF          sort via a key; KEYDEF gives location and type

KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line's end. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options [bdfgiMhnRrV], which override global ordering options for that key. If no key is given, use the entire line as the key.

也很有趣:

-t, --field-separator=SEP use SEP instead of non-blank to blank transition

这告诉我们 F 字段由空格分隔。

关于linux - 如何按第三列中的数字对数据进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35188930/

相关文章:

python - 从子进程更改父进程的目录

linux - RHEL6.5的截图应用

bash - 在ansible中检查shell命令的输出

linux - Bash 数组创建 : ("$@") vs ($@)

c++ - 虚假 "missing sentinel in function call"

linux - 在 unix 中更改 csv 文件分隔符

php - 使用wget远程登录

linux - 在Linux终端中,如何删除目录中除一两个文件外的所有文件

linux - 如何grep一个文件,它是一个从awk重定向的字符串

bash - 我可以通过扩展名制作制表符完成过滤器文件吗?