使用 sort 按数字排序
我有一个日志文件,其中包含以下表单中的条目,我想在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sort -t _ -k 2 -n
,表示:用_
分隔字段,按第二个字段进行数字排序。sort -t _ -k 2 -n
, which means: separate fields by_
, sort numerically by second field.发现
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.