【问题标题】:Sort numerically in VI editor [duplicate]在VI编辑器中按数字排序[重复]
【发布时间】:2012-12-14 02:50:10
【问题描述】:

可能重复:
How to sort numeric and literal columns in Vim

我必须根据线程 ID 对以下行进行排序。

Internal thread 0 bound to OS proc set {1}
Internal thread 1 bound to OS proc set {5}
Internal thread 2 bound to OS proc set {9}
Internal thread 10 bound to OS proc set {41}
Internal thread 9 bound to OS proc set {37}

当我发出:!sort -n 时,它们的排序如下:

Internal thread 0 bound to OS proc set {1}
Internal thread 1 bound to OS proc set {5}
Internal thread 10 bound to OS proc set {41}
Internal thread 2 bound to OS proc set {9}
Internal thread 9 bound to OS proc set {37}

但我需要像这样对它们进行排序:

Internal thread 0 bound to OS proc set {1}
Internal thread 1 bound to OS proc set {5}
Internal thread 2 bound to OS proc set {9}
Internal thread 9 bound to OS proc set {37}
Internal thread 10 bound to OS proc set {41}

【问题讨论】:

  • 在你的情况下,作为:%!sort -n -k 3完成
  • @MichaelBerkowski:感谢它完美运行。请提供此作为答案,以便我接受。我了解n3 选项。但是为什么我需要一个额外的-kn 是否不足以进行数字排序
  • -k 3 明确排序第三个字段。

标签: vim text-editor vi


【解决方案1】:

只需使用 Vim 自己的排序功能。直观地突出显示文本(或使用范围)并输入:

:sort n

文档可在此处获得:http://vim.wikia.com/wiki/Sort_lines

或者在 Vim 本身中::help sort

(经过编辑以反映 dash-tom-bang 的重要说明以及对 Vim 自己的帮助文件的引用。)

【讨论】:

  • 值得注意的是,这里最大的区别并不是删除破折号,而是它使用了 Vim 内置的功能,而不是调用外部应用程序(注意缺少 ! 为好吧)。 Vim 本身也提供文档; :help :sort
  • 我已经编辑了我的答案,因为我认为您的观点足够重要,可以更加突出。
猜你喜欢
  • 2011-04-21
  • 2017-04-04
  • 1970-01-01
  • 2010-12-13
  • 2020-08-30
  • 2014-06-09
  • 2018-04-24
  • 1970-01-01
  • 2014-06-19
相关资源
最近更新 更多