按组和列对行进行排序 [英] Sort lines by group and column
问题描述
我有一个用分号分隔的csv文件.其中包含如下所示的行.而且,我需要按照第一列和第三列的顺序对其进行排序,同时要注意第一列的值所定义的行组.
I have a csv file separated by semicolons. Which contains lines as shown below.. And I need to sort it by the first and third column, respecting the groups of lines defined by the value of the first column.
booke;book;2
booke;booke;1
booke;bookede;6
booke;bookedes;8
booke;booker;4
booke;bookes;7
booke;booket;3
booking;booking;1
booking;bookingen;2
booking;bookingens;3
booking;bookinger;7
booking;bookingerne;5
booking;bookingernes;6
booking;bookingers;8
booking;bookings;4
预期输出:
booke;booke;1
booke;book;2
booke;booket;3
booke;booker;4
booke;bookede;6
booke;bookes;7
booke;bookedes;8
booking;booking;1
booking;bookingen;2
booking;bookingens;3
booking;bookings;4
booking;bookingerne;5
booking;bookingernes;6
booking;bookinger;7
booking;bookingers;8
我尝试使用 sort -t;-k3,3n -k1,1
,但按整个第三列排序.
I tried it with sort -t; -k3,3n -k1,1
but it's sorted by the third entire column.
推荐答案
以管道方式使用两种排序方法:
What about using two sorts in a pipeline fashion:
sort -t ';' -k 3,3n | sort -t ';' -k 1,1 -s
为了启用稳定排序,第二个参数中的 -s
是必需的.否则,它可能会破坏先前的(第三列)排序.
The -s
in the second parameter is necessary in order to enable stable sort. Otherwise it could destroy the previous (third column) sorting.
编辑:但是为@BenjaminW.在他的评论中指出,您可以使用多个 -k
标志,则仅以错误的方式指定了它们.通过执行排序:
EDIT: however as @BenjaminW. points out in his comment, you can use multiple -k
flags, you only specified them the wrong way. By performing a sort:
sort -t ';' -k 1,1 -k 3,3n
第一列为主要排序列,第三列为第二列.
It takes the first column als primary sorting column and the third as secondary.
这篇关于按组和列对行进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!