我试图根据日期和时间对这两行进行反向排序,我使用下面的命令,它工作正常,但没有考虑上午和下午,所以下面两行没有正确排序,因为第一行的时间是上午 10 点,第二个是下午 2 点
命令:
sort -t',' -k2,2 -k7,7r file.csv
行:
排序前:
field1,field2,field3,field4,field5,field6,19-NOV-23 10.58.03.000000 AM
field1,field2,field3,field4,field5,field6,19-NOV-23 02.48.47.000000 PM
排序后的行(相同排列)
field1,field2,field3,field4,field5,field6,19-NOV-23 10.58.03.000000 AM
field1,field2,field3,field4,field5,field6,19-NOV-23 02.48.47.000000 PM
man sort
:
-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. - -
数据中的字段 7:
1111111111222222222
1234567890123456789012345678
19-NOV-23 10.58.03.000000 AM
19-NOV-23 02.48.47.000000 PM
首先定义反向、字段分隔符和年份(字段 7,位置 8 和 9,数字):
sort -r -t, -k7.8,7.9n file
然后是月份:
sort -r -t, -k7.8,7.9n -k7.4,7.6M file
然后是当天和 AM/PM(实际上是 A/P):
sort -r -t, -k7.8,7.9n -k7.4,7.6M -k7.1n,7.2n -k7.27 file
然后是时间部分(留给练习)。
通过以上你可以得到:
field1,field2,field3,field4,field5,field6,19-NOV-23 02.48.47.000000 PM
field1,field2,field3,field4,field5,field6,19-NOV-23 10.58.03.000000 AM