使用 sort 按数字排序

发布于 2024-12-08 15:31:20 字数 404 浏览 1 评论 0原文

我有一个日志文件,其中包含以下表单中的条目,我想在 ID 号字段上对这些条目进行数字排序。

2011-10-06 08:13:48 ID_39 message1
2011-10-06 09:13:5s ID_239 message2

我尝试使用 sort 命令来完成此操作,但我似乎没有找到正确的键。使用 sort -b -k 3 排序只是按字母顺序排序,而 -n (数字排序)似乎没有帮助。

我如何从这个

ID_394
ID_65
ID_9
ID_99

到这个?

ID_9
ID_65
ID_99
ID_394

I have a logfile with entries on the following form that I want to sort numerically on the ID number field.

2011-10-06 08:13:48 ID_39 message1
2011-10-06 09:13:5s ID_239 message2

I have tried to accomplish this with the sort command but I don't seem to get the keys right. Sorting with sort -b -k 3 just sort things alphabetically and -n (numeric sort) does not seem to help.

How do I go from this

ID_394
ID_65
ID_9
ID_99

To this?

ID_9
ID_65
ID_99
ID_394

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抽个烟儿 2024-12-15 15:31:20

sort -t _ -k 2 -n,表示:用_分隔字段,按第二个字段进行数字排序。

sort -t _ -k 2 -n, which means: separate fields by _, sort numerically by second field.

贱人配狗天长地久 2024-12-15 15:31:20

发现 sort -b -k 3.4 -n 也达到了目的。使用 -b 忽略前导空格,使用 -k 3.4 从第四个字母开始对第三个字段进行排序,使用 -n 进行数字排序。

Found out that also sort -b -k 3.4 -n did the trick. With -b for ignoring leading blanks, -k 3.4 to sort for the third field, from the fourth letter and -n for numerically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文