如何对列进行分层排序?
问题描述:
我怎样才能按2个独立的列排序?例如,我想排序列6,再排序列4如何对列进行分层排序?
column4 column5 column6
lae2894 603 user1
e2894 2096 user1
e2894 2096 user1
e2894 2096 user1
lae2894 603 user1
lae2894 603 user1
那些已经通过以下命令进行排序:
sort -t, -k6 users.txt > sorted-user.txt
但我想应该是这样的输出:
column4 column5 column6
e2894 603 user1
e2894 2096 user1
e2894 2096 user1
laee2894 2096 user1
lae2894 603 user1
lae2894 603 user1
答
sort
的大多数版本允许多个关键规范。试试这个:
我假设你的实际输入文件使用,
作为分隔符,因为你指定在你的榜样命令(这意味着您贴上了“榜样”的数据是不是真的代表你的文件...)。还要注意,只有一个数字的密钥规范(如-k6
)意味着一个在该字段中开始并延伸到行尾的密钥,因此为了指定单个字段进行排序,您需要使用上面的语法。
答
sort
的-k
选项可能会出现多次。请尝试:
sort -t, -k6,6 -k4,4 inputfile
有关更多信息,请参阅sort invocation。
> ‘-k pos1[,pos2]’
> ‘--key=pos1[,pos2]’
Specify a sort field that consists of the part of the line between pos1 and pos2 (or the end of the line, if pos2 is omitted), inclusive.
Each pos has the form ‘f[.c][opts]’, where f is the number of the field to use, and c is the number of the first character from the beginning of the field. Fields and character positions are numbered starting with 1; a character position of zero in pos2 indicates the field's last character. If ‘.c’ is omitted from pos1, it defaults to 1 (the beginning of the field); if omitted from pos2, it defaults to 0 (the end of the field). opts are ordering options, allowing individual keys to be sorted according to different rules; see below for details. Keys can span multiple fields.
Example: To sort on the second field, use --key=2,2 (-k 2,2). See below for more notes on keys and more examples. See also the --debug option to help determine the part of the line being used in the sort.
+0
谢谢devnull。我测试了你的发布。有用。 – user2452340
欢迎来到stackoverflow,但你在谈论什么软件或编程语言? –
他正在谈论unix排序工具。 – XapaJIaMnu
@phresnel unix/linux shell – SheetJS