gpt4 book ai didi

bash - 对行组进行排序

转载 作者:行者123 更新时间:2023-11-29 09:04:02 24 4
gpt4 key购买 nike

假设我有这个列表:

sharpest
tool
in
the
shed
im
not
the

如何按非缩进行的字母顺序排序并保留行组?上面应该变成:

im
not
the
sharpest
tool
in
the
shed

存在类似问题herehere但我似乎无法让它们为我的示例工作。

到目前为止有希望的想法

  • 也许我可以用某种方式使用 grep -n,因为它会给我行号?我想先得到行号,然后再订购。我想我在订购之前需要以某种方式计算行范围,然后从那里以某种方式获取行范围。然而,甚至想不出该怎么做!
  • 范围看起来也很有希望,但同样的交易; sed 1,2pfurther examples here .

最佳答案

如果 perl 没问题:

$ perl -0777 -ne 'print sort split /\n\K(?=\S)/' ip.txt
im
not
the
sharpest
tool
in
the
shed
  • -0777 吞噬整个文件,因此如果输入太大则解决方案不合适
  • split/\n\K(?=\S)/ 使用换行符后跟非空白字符作为拆分指示给出数组
  • sort 对数组进行排序

关于bash - 对行组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48925359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com