使用sed获取第n次出现标记的标记值

时间:2014-11-05 16:40:08

标签: sed

MY xml

<?xml version="1.0" encoding="UTF-8" ?>
<Attributes>
   <Attribute>123</Attribute>
   <Attribute>959595</Attribute>
   <Attribute>1233</Attribute>
   <Attribute>jiji</Attribute>
</Attributes>

我需要使用sed

获取属性标记第二次出现的标记值,即959595

我使用了命令

sed -n ':a;$!{N;ba};s#\(<Attribute\)\(.*\)\(</Attribute>\)#\1#2#\2#p' file

模式一秒发生模式两个值它不起作用

我不知道我的方法是否正确请更正我的命令

4 个答案:

答案 0 :(得分:2)

这样做的正确方法是:

$ xmllint --xpath '/Attributes/Attribute[2]/text()' file.xml

注意

  • xmllint附带libxml2
  • &#39; 2&#39;是第二个被搜索的元素

答案 1 :(得分:0)

sed -n '/<Attributes>/,\#</Attributes># {
  /<Attribute>/ {
     H;g
     s#.*<Attribute>\(.*\)</Attribute>.*#\1#
     t found
     }
   b
:found
   p;q
   }' YourFile
  • 假设,就像你的样本中一样,只有1个属性被找到,这个sed只返回第1个。 (如果xml内容仅与您的样本类似,则不需要/<Attributes>/,\#</Attributes>#选择)
  • 在GNU sed
  • 上的Posix版本--posix

答案 2 :(得分:0)

此sed打印属性块中的所有属性条目,然后获取第二个条目并删除标记:

sed -n '/<Attributes>/,\#</Attributes>#{/<Attribute>/p}' attrib.txt | sed -n '2p' | sed 's#</Attribute>##;s/<Attribute>//'

Output: 
   959595

或者没有管道的另一种方法是使用sed命令,这将转到第二个条目剥离Attribute标签,然后退出:

sed -n '/<Attributes>/,\#</Attributes>#{/<Attribute>/{n;s#.*<Attribute>\(.*\)</Attribute>.*#\1#;p;q};}' attrib.txt

或者,如果您的属性条目数量发生变化,您可以通过解析所有值然后使用sed打印所需的属性位置来使其更直观:

sed -n '/<Attributes>/,\#</Attributes>#{/<Attribute>/{s#</Attribute>##;s#<Attribute>##;p}}' attrib.txt | sed -n '2p'

您可以更改从2到您要显示的属性值字段的结尾,或者使用多个值,例如sed -n '2p;3p'sed -n '1,2p'

答案 3 :(得分:0)

我也将遵循xmllint xpath的方式。但是,似乎有两个版本可用。根据{{​​3}}上的手册页,没有xpath参数,但它称为“模式”。

按照此文档操作,您的电话将会是

$ xmllint --pattern '/Attributes/Attribute[2]/text()' file.xml

我建议检查您的本地手册页以查看使用哪个页面。