【问题标题】:Sed to remove user specified blocksed 删除用户指定的块
【发布时间】:2015-03-15 03:14:07
【问题描述】:

我正在使用一个很长的 .XML 文件(大约 60K 行)。我需要 bash 来启动脚本并让用户输入要从 .XML 文件中删除的名称。我在想sed,但如果有更好的选择,我也愿意。这是我到目前为止所得到的:

echo -n "Type media to remove and press [ENTER]"
read TARGET

while true; do
    read -p "Are you sure you wish to remove $TARGET from the system?" yn
    case $yn in
        [Yy]* ) SED COMMAND HERE; break;;
        [Nn]* ) echo "Cancelling..."; exit;;
    * ) echo "---please answer [Y] or [N]";;
    esac
done

这里是 .XML 文件的一部分。请注意,我发布的这一部分在 .XML 中重复了数百次。块中的唯一区别是我在此示例中标记为“corrupt”。

<media>
  <name>"corrupt"</name>
  <parent>system</parent>
  <location>/path/to/the/"corrupt".zip</location>
  <video>/another/path/"corrupt".flv</video>
  <images>
    <image>
      <type>saved</type>
      <image-file>/yet/another/path/"corrupt".png</image-file>
    </image>
  </images>
</media>

在本例中,我希望从 .XML 文件中删除“corrupt”。我认为重要的是要说 .XML 文件中只有 1 个“corrupt”实例。此外,对于其他“corrupt_files”,文件名中没有空格,只有下划线或破折号。

所以sed 需要删除包含“corrupt”信息的整个 xml 块,在删除文本的地方不留空行,然后脚本将覆盖当前的“media.xml”文件.

我希望这个问题不会令人困惑。

【问题讨论】:

  • 你写'所以 sed 需要删除包含“损坏”信息的整个 xml 块'; “整个 xml 块”是什么意思?您是指&lt;location&gt;...&lt;/location&gt; 和&lt;video&gt;...&lt;/video&gt; 元素,还是指整个&lt;media&gt;...&lt;/media&gt; 元素?正如@Jubobs 指出的那样,无论哪种方式,sed 都可能是该工作的错误工具,但您所指的工具会影响答案。
  • @Brian ...对不起。通过块,我的意思是我发布的整个 xml 示例。这就是我所说的块。因此,包括 ... 在内的所有内容
  • @jubobs 我不必使用 sed ,这正是我认为可行的方法

标签: xml bash sed


【解决方案1】:

您应该使用正确的xml 工具,但是这个gnu awk 删除了name 包含corrupt 的块

cat file
<media>
  <name>"test1"</name>
  <parent>system</parent>
  <location>/path/to/the/"test1".zip</location>
  <video>/another/path/"test1".flv</video>
  <images>
    <image>
      <type>saved</type>
      <image-file>/yet/another/path/"test1".png</image-file>
    </image>
  </images>
</media>
<media>
  <name>"corrupt"</name>
  <parent>system</parent>
  <location>/path/to/the/"corrupt".zip</location>
  <video>/another/path/"corrupt".flv</video>
  <images>
    <image>
      <type>saved</type>
      <image-file>/yet/another/path/"corrupt".png</image-file>
    </image>
  </images>
</media>
<media>
  <name>"test2"</name>
  <parent>system</parent>
  <location>/path/to/the/"test2".zip</location>
  <video>/another/path/"test2".flv</video>
  <images>
    <image>
      <type>saved</type>
      <image-file>/yet/another/path/"test2".png</image-file>
    </image>
  </images>
</media>

awk -v RS="<media>" '!/<name>"corrupt/ && NR>1 {print RS$0}'
<media>
  <name>"test1"</name>
  <parent>system</parent>
  <location>/path/to/the/"test1".zip</location>
  <video>/another/path/"test1".flv</video>
  <images>
    <image>
      <type>saved</type>
      <image-file>/yet/another/path/"test1".png</image-file>
    </image>
  </images>
</media>

<media>
  <name>"test2"</name>
  <parent>system</parent>
  <location>/path/to/the/"test2".zip</location>
  <video>/another/path/"test2".flv</video>
  <images>
    <image>
      <type>saved</type>
      <image-file>/yet/another/path/"test2".png</image-file>
    </image>
  </images>
</media>

【讨论】:

  • 似乎每当我cd 归档并运行awk -v RS="&lt;media&gt;" '!/&lt;name&gt;"corrupt/ &amp;&amp; NR&gt;1 {print RS$0}' 时什么都没有发生。动作开始,但我必须ctrl+c 让它退出并且要更改的 .xml 文件与以前相同。也许我忽略了什么?
  • 哈哈...天哪。但是,cat test.xml &gt; awk -v RS="&lt;media&gt;" '!/&lt;name&gt;"corrupt/ &amp;&amp; NR&gt;1 {print RS$0}' test.xml &gt; test-new.xml 似乎只是在每个块之间添加了一个空格。
  • 啊!只需删除'!/&lt;name&gt;"corrupt/ 中的符号“ 这似乎可以很好地删除损坏的块并在块之间添加一个空格,但我可以只使用另一个命令来删除空白行。
  • @GHO5TLIKE5WAYZE 像这样设置RS 的一个负面因素是空行。您可以稍后使用 sed 或 awk 将其删除。但一个好的规则是不要将cat 与sed/awk 或其他可以读取数据本身的程序一起使用。 awk -v RS="&lt;media&gt;" '!/&lt;name&gt;"corrupt/ &amp;&amp; NR&gt;1 {print RS$0}' test.xml &gt; test-new.xml 是一个很好的方法。
  • 但是现在出现了一个新问题....有标签位于块的上方和下方,就像&lt;file&gt; &lt;media-list&gt; {xml blocks} &lt;/media-list&gt; &lt;/file&gt; 似乎该命令将所有这些标签连同“损坏”块一起剥离。只留下&lt;media&gt; 块。我想我可以在覆盖原始文件之前将它们sed 重新放入文件中。
猜你喜欢
  • 2011-10-20
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 2016-11-08
相关资源
最近更新 更多