【问题标题】:sed with newlines and "带有换行符的 sed 和 "
【发布时间】:2012-04-25 14:51:42
【问题描述】:

我有一堆看起来像这样的文本文件:

His doctor attributed this to an allergy .

That hardly convinced him , as he had no history of allergies of any kind .
" Yet , that was to be the least of his problems .
I may have to take steroids for the rest of my life .
"
A topical steroid spray was later added to his repertoire of drugs and 
" he knew it was merely masking the underlying condition .
"

我想改变它,使. " 在一行中。所需的输出应如下所示:

His doctor attributed this to an allergy .

That hardly convinced him , as he had no history of allergies of any kind .
" Yet , that was to be the least of his problems .
I may have to take steroids for the rest of my life . "
A topical steroid spray was later added to his repertoire of drugs and 
" he knew it was merely masking the underlying condition . "

我试过了,但它不起作用:

sed -i 's/.\n"\n/. "\n/g'

有人可以帮助我使用正确的 sed 命令将 " 向上移动吗?

【问题讨论】:

  • 它不起作用的原因是sed单独在行上工作。
  • 那么有没有其他非sed的方式来解决呢??
  • 这将强制出现带有 " 的第二行。您不能明确查找换行符。现在,下一步是去掉引号并加入这两行。尝试替换时或搜索 \n,改用 $。这是连续处理两行的方法。我看到了 perl 解决方案,这可能是一个好方法。sed -e '/\.$/ { N /"/p }' test.txt N和/"/p}'之间有一个换行符。

标签: regex sed newline


【解决方案1】:

这是我想出来的:

sed -n '1{h;d};/^"$/{g;s/$/ "/p;n;h;d};x;p;${g;p}' input.txt

输出

His doctor attributed this to an allergy .

That hardly convinced him , as he had no history of allergies of any kind .
" Yet , that was to be the least of his problems .
I may have to take steroids for the rest of my life . "
A topical steroid spray was later added to his repertoire of drugs and
" he knew it was merely masking the underlying condition . "

【讨论】:

  • 但我需要保留 \n 的其余部分,只有带有 "\n 的部分需要被截断。
  • 感谢它创造了奇迹!!但为什么会这样呢?为什么需要这么复杂的正则表达式?
  • 就像用汇编语言编程一样。您应该跟踪每一个微小的步骤。
  • h;d;g;x;psed 命令。您可以输入man sed 以了解更多信息。
  • 最后一个问题,我如何为一堆文件编写和编写它?以前我使用sed -i ... * 更改我的txtfiles 中的正则表达式。 -n 仅显示文本 -i,而您的正则表达式仅写入弄乱了 txtfiles。
【解决方案2】:
perl -00 -lpe 's/\n"$/"/mg'

产生所需的输出。

【讨论】:

    【解决方案3】:

    一个略有不同的sed 变体:

    sed -n '1{h};1!{/"$/!H};/"$/{H;g;s/\.[ \n]*"$/\. "/;p;n;x}' input.txt
    
    • 1 { h } — 将第一行放入保持缓冲区
    • 1! { /"$/ !H } — 对于其余的行,如果没有孤独,则累积到保持缓冲区中 "
    • /"$/ { H; g; s/\.[ \n]*"$/\. "/; p; n; x } — 否则:

      1. H — 添加到保持缓冲区
      2. g — 将保持缓冲区移动到模式空间
      3. s/\.[ \n]*"$/\. "/ — 替换
      4. p — 打印出来
      5. n — 阅读下一行
      6. x——并将其保存在保留缓冲区中

    【讨论】:

      【解决方案4】:

      这可能对你有用:

      sed ':a;$!N;s/\.\n"/."/;P;D' /tmp/a
      His doctor attributed this to an allergy .
      
      That hardly convinced him , as he had no history of allergies of any kind ." Yet , that  was to be the least of his problems .
      I may have to take steroids for the rest of my life ."
      A topical steroid spray was later added to his repertoire of drugs and 
      " he knew it was merely masking the underlying condition ."
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-08
        • 2021-09-21
        • 2016-10-29
        • 1970-01-01
        • 2017-09-25
        • 2019-12-01
        • 2013-04-13
        • 2020-05-23
        相关资源
        最近更新 更多