【问题标题】:Delete lines based on keywords within tags根据标签内的关键字删除行
【发布时间】:2013-06-20 13:52:35
【问题描述】:

我有一个目录 .txt 文件作为搜索和提取的输出生成 程序。 .txt 文件格式如下。

基于关键字Entrust,我正在尝试使用以下行删除 sed 作为后处理步骤。

<content>This document has been digitally signed with external signatures using Entrust PKI</content>

我在 shell 脚本中运行的 sed 命令如下:注意到 .txt 文件中没有删除任何行。 sed不能根据标签内的内容进行搜索和删除吗?有没有其他方法可以做到这一点?

sed '/Entrust/d' $file > ${file}.mod;
  <block>
  <title>
This is the title
  </title>
  </block>
  <block>
  <content>
Content1
  </content>
  </block>
  <block>
  <title>
Title 2
  </title>
  <content>
some content 2
  </content>
  </block>
  <block>
  <title>
Title 3
  </title>
  <content>
some content 3
  </content>
  <content>
This document has been digitally signed with external signatures using Entrust PKI

  </content>
  <content>
some content4

  </content>
  <content>
This document has been digitally signed with external signatures using Entrust PKI
  </content>
 </block>

【问题讨论】:

  • 文本文件说 Entrust,你的 sed cmd 寻找 Entrusted
  • @simak 文本文件是在windows上创建的吗?您是要删除包含Entrust 的行还是删除包含该行的标签?
  • 我想删除包含 Entrust 的行。谢谢!

标签: linux shell unix sed


【解决方案1】:

据我了解,您正在尝试从 txt 文件中删除。我会为此建议sed -i。您应该看到使用您的命令在${file}.mod 中删除了所需的行

sed -i '/Entrust/d' $file

【讨论】:

    【解决方案2】:

    你可以试试:

    sed -n '/Entrust/!p' $file > ${file}.mod
    

    sed '/Entrust/d' $file > ${file}.mod
    

    awk '!/Entrust/' $file > ${file}.mod
    

    【讨论】:

      【解决方案3】:

      如前所述,您的文件包含Entrust,但您正在搜索Entrusted

      sed '/Entrust/d'
      

      【讨论】:

      • 抱歉输入这里时打错了。我正在运行的命令确实是: sed '/Entrust/d' $file > ${file}.mod;
      • @simak 如果有这样的严重错字,请编辑您的问题(我这次是为您做的)。
      【解决方案4】:
      sed -i 's/<content>This document has been digitally signed with external signatures using Entrust PKI</content>/#<content>This document has been digitally signed with external signatures using Entrust PKI</content>/g' $filename 
      

      这是您可以注释脚本不会注意到/读取的行的方式。

      【讨论】:

        【解决方案5】:
        perl -lne 'print unless(/\bEntrust\b/)' your_file.txt > your_file.mod
        

        【讨论】:

          【解决方案6】:

          要搜索 XML 样式标签之外的文本,请使用以下命令;

          sed '/^\([^<]*\(<[^<>]*>\)*\)*Entrust/d'
          

          这是一个例子;

          $ cat tmp.txt
          some content 2.
          some content with Entrust.
          <tag type='Entrust'/>
          <tag>Entrust</tag>
          $ sed '/^\([^<]*\(<[^<>]*>\)*\)*Entrust/d' tmp.txt 
          some content 2.
          <tag type='Entrust'/>
          

          请注意,此表达式不处理包含换行符的标记。

          【讨论】:

            猜你喜欢
            • 2022-01-20
            • 1970-01-01
            • 2019-12-27
            • 2013-12-24
            • 1970-01-01
            • 1970-01-01
            • 2017-10-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多