【问题标题】:Hold buffer to rearrange texts保持缓冲区重新排列文本
【发布时间】:2013-12-10 15:43:17
【问题描述】:

我不知道这样做的好方法(参见/awk/perl);我结合了html文件的多个章节,它具有以下结构

 <a href="#chapter11">title</a>
 <a href="#chapter12">title</a>
 <a href="#chapter13">title</a>
 <p>first chapter contents, multiple
 pages</p>
 <a href="#chapter21">title</a>
 <a href="#chapter22">title</a>
 <a href="#chapter23">title</a>
 <p>Second chapter contents, multiple pages
 more informations</p>
 <a href="#chapter31">title</a>
 <a href="#chapter32">title</a>
 <a href="#chapter33">title</a>
 <p>Third chapter contents, multiple pages
 few more details</p>

我希望他们像下面这样重新组织

 <a href="#chapter11">title</a>
 <a href="#chapter12">title</a>
 <a href="#chapter13">title</a>
 <a href="#chapter21">title</a>
 <a href="#chapter22">title</a>
 <a href="#chapter23">title</a>
 <a href="#chapter31">title</a>
 <a href="#chapter32">title</a>
 <a href="#chapter33">title</a>
 <p>first chapter contents, multiple
 pages</p>
 <p>Second chapter contents, multiple pages
 more informations</p>
 <p>Third chapter contents, multiple pages
 few more details</p>

我在一个 html 中有五个章节来重新组织它们。我试图采用 sed 保持缓冲区,但据我所知,这似乎很困难。我不限于 sed 或 awk。任何帮助将不胜感激,谢谢。

编辑

抱歉修改了源文件,它也有几行并不总是以

  <a or <p

在 sed 中是否有类似逆向选择的脚本,比如

 /^<a!/p/

【问题讨论】:

    标签: bash sed awk


    【解决方案1】:

    运行两次怎么样,先输出&lt;a&gt;标签,再输出&lt;p&gt;标签:

    sed -n '/^<a/p' input.txt
    sed -n '/^<p/p' input.txt
    

    使用holdspace可以这样做:

    sed -n '/^<a/p; /^<p/H; ${g; s/\n//; p}' input.txt
    

    打印所有&lt;a&gt;标签,将所有&lt;p&gt;标签放入holdspace,在文档末尾($),获取holdspace并打印。 H 总是在追加到保持空间之前添加一个换行符,这是我们不想要的第一个换行符,这就是我们使用 s/\n// 删除它的原因。

    如果你想存储输出,你可以重定向它

    sed -n '/^<a/p; /^<p/H; ${g; s/\n//; p}' input.txt > output.txt
    

    要直接使用sed -i,我们需要稍微重构一下代码:

    sed -i '${x; G; s/\n//; p}; /^<p/{H;d}' input.txt
    

    但这有点乏味。

    如果你有以其他字符开头的行,并且只想将所有以&lt;a&gt;标签开头的行移到前面,你可以这样做

    sed -n '/^<a/p; /^<a/! H; ${g; s/\n//; p}' input.txt
    

    【讨论】:

    • 通过包含 (bz of mac BSD sed) 运行良好;在关闭大括号之前,如 (sed -n '/^
    • 编辑了我的答案。 sed -i 也应该可以工作,但需要对代码进行一些重组。
    • 感谢 pfnuessel,我的源文件也有几行并不总是以
    • 再次编辑。请从一开始就尽可能完整地描述您的问题。在某些时候使用awk 解决方案可能会更容易。
    【解决方案2】:

    Grep 也可以:

    (grep -F '<a' test.txt ; grep -F '<p' test.txt)
    

    【讨论】:

    【解决方案3】:
    sed -n '/^ *<[aA]/ !H
    /^ *<[aA]/ p
    $ {x;s/\n//;p;}
    ' YourFile
    

    如果

    【讨论】:

    【解决方案4】:

    使用awk

    awk '{if ($0~/<a/) a[NR]=$0; else b[NR]=$0} END {for (i=1;i<=NR;i++) if (a[i]) print a[i];for (j=1;j<=NR;j++) if (b[j]) print b[j]}' file
     <a href="#chapter11">title</a>
     <a href="#chapter12">title</a>
     <a href="#chapter13">title</a>
     <a href="#chapter21">title</a>
     <a href="#chapter22">title</a>
     <a href="#chapter23">title</a>
     <a href="#chapter31">title</a>
     <a href="#chapter32">title</a>
     <a href="#chapter33">title</a>
     <p>first chapter contents, multiple
     pages</p>
     <p>Second chapter contents, multiple pages
     more informations</p>
     <p>Third chapter contents, multiple pages
     few more details</p>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-24
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多