GNU 排序和 git 责备

Car*_*rum 4 unix git gnu sorting cut

今天我以为我在做一个简单的操作:

git blame file | sort -k 3
Run Code Online (Sandbox Code Playgroud)

获取file按最后更改日期排序的所有行。不幸的是,它似乎没有正确排序。如果我执行以下操作,它确实有效:

git blame file | cut -c 20- | sort
Run Code Online (Sandbox Code Playgroud)

这只是将线路的第一部分砍掉,然后进行排序。该命令的成功表明,我认为sort 可以做实际上我是想操作。为什么不起作用?

这是来自输出的示例行git blame file

35d8e9eb (username 2007-01-17 03:58:04 +0000 155) Some text on line 155
Run Code Online (Sandbox Code Playgroud)

编辑:除了3在原始命令行中使用任何合理的数字似乎工作正常 - 我可以按哈希(字段 1)、用户名(字段 2)、一天中的时间(字段 4)或行号(字段6),但日期(字段 3)不起作用 - 我根本无法确定它实际上以什么顺序结束......

编辑 2:我制作了一个显示相同问题的经过消毒的输入文件。这是cleaned.txt

cb7bb041 (eeeeeee  2010-12-14 19:41:18 +0000  42)
35d8e9eb (cccccccc 2007-01-17 03:58:04 +0000 135)
d7377fa9 (hhhhhhhh 2010-01-30 04:26:28 +0000 178)
Run Code Online (Sandbox Code Playgroud)

和输出sort -k 3 cleaned.txt

$ sort -k 3 cleaned.txt 
cb7bb041 (eeeeeee  2010-12-14 19:41:18 +0000  42)
35d8e9eb (cccccccc 2007-01-17 03:58:04 +0000 135)
d7377fa9 (hhhhhhhh 2010-01-30 04:26:28 +0000 178)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,日期字段上的排序似乎根本没有发生。这是来自的输出cut -c 20- cleaned.txt | sort

$ cut -c 20- cleaned.txt | sort
2007-01-17 03:58:04 +0000 135)
2010-01-30 04:26:28 +0000 178)
2010-12-14 19:41:18 +0000  42)
Run Code Online (Sandbox Code Playgroud)

在这种情况下按日期排序工作正常!有什么建议?

Car*_*rum 6

我只是想通了。较短的用户名eeeeeee意味着在日期字段之前有一个额外的空间。由于字段分隔符sort是非空白到空白的转换,用户名较短的行的日期字段将该空格作为键字段的一部分,并首先排序。简单的修复:

git blame file | sort -b -k 3
Run Code Online (Sandbox Code Playgroud)

  • 您还必须注意其中包含不同空格数的作者姓名。你的例子都有单字作者姓名(`eeeeeee`、`cccccccc` 和`hhhhhhhh`),但真实姓名通常会有所不同(例如,git.git 的作者姓名少至 1 个,多至 5 个空格分隔“字”)。使用 `-e` 可能会有所帮助;它显示的电子邮件地址不太可能有任何空格。 (3认同)