如果我有一个由如下所示的数据组成的文件,我将如何根据第三列中的数字对数据进行排序?
前两列之间的空格不是制表符分隔的,而是一些空格。第二列和第三列之间的间距根据数字的大小而变化。
另请注意,第二列的某些数据中有空格(例如 (
和 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/