如何对线组进行排序? [英] How to sort groups of lines?

查看:76
本文介绍了如何对线组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,必须对3个元素进行排序:

In the following example, there are 3 elements that have to be sorted:

  1. "[aaa]"和它下面的4条线(总是4条)形成一个单元.
  2. "[kkk]"和它下面的4条线(总是4)形成一个单元.
  3. "[zzz]"和它下面的4条线(总是4)形成一个单元.

仅对遵循此模式的几组行进行排序;在"[aaa]"之前和"[zzz]"的第四行之后的所有内容都应保持不变.

Only groups of lines following this pattern should be sorted; anything before "[aaa]" and after the 4th line of "[zzz]" must be left intact.

来自:

This sentence and everything above it should not be sorted.

[zzz]
some
random
text
here
[aaa]
bla
blo
blu
bli
[kkk]
1
44
2
88

And neither should this one and everything below it.

收件人:

This sentence and everything above it should not be sorted.

[aaa]
bla
blo
blu
bli
[kkk]
1
44
2
88
[zzz]
some
random
text
here

And neither should this one and everything below it.

推荐答案

也许不是最快的方法:) [1]但我相信它将满足您的要求

Maybe not the fastest :) [1] but it will do what you want, I believe:

for line in $(grep -n '^\[.*\]$' sections.txt |
              sort -k2 -t: |
              cut -f1 -d:); do
  tail -n +$line sections.txt | head -n 5
done

这是一个更好的选择:

for pos in $(grep -b '^\[.*\]$' sections.txt |
             sort -k2 -t: |
             cut -f1 -d:); do
  tail -c +$((pos+1)) sections.txt | head -n 5
done


[1]第一个类似于文件的行数,类似于O(N ^ 2),因为它必须一直读取每个节的节.第二个可以立即找到正确字符位置的字符,应该更接近O(N log N).


[1] The first one is something like O(N^2) in the number of lines in the file, since it has to read all the way to the section for each section. The second one, which can seek immediately to the right character position, should be closer to O(N log N).

[2]这使您一口气说每个部分中总是正好有五行(标题加四行),因此为head -n 5.但是,如果确实有必要,将其替换为一个读到但不包括以'['开头的下一行的内容,真的很容易.

[2] This takes you at your word that there are always exactly five lines in each section (header plus four following), hence head -n 5. However, it would be really easy to replace that with something which read up to but not including the next line starting with a '[', in case that ever turns out to be necessary.

保留开始和结束需要更多的工作:

Preserving start and end requires a bit more work:

# Find all the sections
mapfile indices < <(grep -b '^\[.*\]$' sections.txt)
# Output the prefix
head -c+${indices[0]%%:*} sections.txt
# Output sections, as above
for pos in $(printf %s "${indices[@]}" |
             sort -k2 -t: |
             cut -f1 -d:); do
  tail -c +$((pos+1)) sections.txt | head -n 5
done
# Output the suffix
tail -c+$((1+${indices[-1]%%:*})) sections.txt | tail -n+6

您可能要使用其中的功能或脚本文件,将sections.txt始终更改为$ 1.

You might want to make a function out of that, or a script file, changing sections.txt to $1 throughout.

这篇关于如何对线组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆