添加/使用bash脚本删除XML标记 [英] Add/remove xml tags using a bash script

查看:263
本文介绍了添加/使用bash脚本删除XML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想用一个bash脚本配置XML文件。例如,如果我有这样的XML:

I have an xml file that I want to configure using a bash script. For example if I had this xml:

<a>

  <b>
    <bb>
        <yyy>
            Bla 
        </yyy>
    </bb>
  </b>

  <c>
    <cc>
      Something
    </cc>
  </c>

  <d>
    bla
  </d>
</a>

(信息保密删除)

我想编写一个bash脚本,将删除部分&LT; B&GT; (或注释),但可保留完整的XML的其余部分。我是pretty新的整个脚本的事情。我想知道如果任何人都可以给我一个提示,我应该看看成什么样。

I would like to write a bash script that will remove section <b> (or comment it) but keep the rest of the xml intact. I am pretty new the the whole scripting thing. I was wondering if anyone could give me a hint as to what I should look into.

我在想,SED可用于的除了的sed是一个行编辑器。我认为这将是很容易删除&LT; B&GT; 标签但是我不能确定,如果用sed将能够去除所有的文字 &LT; b&GT; 标记。

I was thinking that sed could be used except sed is a line editor. I think it would be easy to remove the <b> tags however I am unsure if sed would be able to remove all the text between the <b> tags.

我还需要编写一个脚本来重新添加删除的部分。

I will also need to write a script to add back the deleted section.

推荐答案

这不会是很难在SED做,因为sed的也适用于范围。

This would not be difficult to do in sed, as sed also works on ranges.

试试这个(假设XML是在一个名为foo.xml文件):

Try this (assuming xml is in a file named foo.xml):

sed -i '/<b>/,/<\/b>/d' foo.xml

-i会写换入原始文件(使用-i.bak保留的备份副本原件)

-i will write the change into the original file (use -i.bak to keep a backup copy of the original)

这sed命令将在所有由范围指定的行执行操作D(删除)

This sed command will perform an action d (delete) on all of the lines specified by the range

# all of the lines between a line that matches <b>
# and the next line that matches <\/b>, inclusive
/<b>/,/<\/b>/

所以,用简单的英语,该命令将删除所有的线,并包括以&lt行; B&GT;并以&lt行; / B个

So, in plain English, this command will delete all of the lines between and including the line with <b> and the line with </b>

如果你宁愿行注释掉,请尝试以下之一:

If you'd rather comment out the lines, try one of these:

# block comment
sed -i 's/<b>/<!-- <b>/; s/<\/b>/<\/b> -->/' foo.xml

# comment out every line in the range
sed -i '/<b>/,/<\/b>/s/.*/<!-- & -->/' foo.xml

这篇关于添加/使用bash脚本删除XML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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