gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 12:01:17 30 4
gpt4 key购买 nike

如果我有一个包含如下数据的文件,我将如何根据第三列中的数字对数据进行排序?

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

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

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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com