对线组进行排序

Nic*_*ull 5 bash shell awk grep sed

说我有这个清单:

sharpest
  tool
  in
the
  shed
im
  not
  the
Run Code Online (Sandbox Code Playgroud)

如何通过非缩进行按字母顺序排序并保留行组?以上应该成为:

im
  not
  the
sharpest
  tool
  in
the
  shed
Run Code Online (Sandbox Code Playgroud)

这里这里也存在类似的问题,但我似乎无法让它们适用于我的例子.

到目前为止有希望的想法

  • 也许我可以用grep -n某种方式,因为它给了我行号?我想先得到行号,然后订购.我想我在某种程度上需要在订购之前计算一个行范围,然后从那里以某种方式获取行的范围.甚至不能想到如何做到这一点!
  • 范围看起来很有希望,但同样的交易; 这里sed 1,2p还有其他例子.

Sun*_*eep 3

如果perl没问题的话:

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