做到从UNIX命令行基本的XML解析最简单的方法 [英] Simplest way to do basic xml parsing from unix command line

查看:403
本文介绍了做到从UNIX命令行基本的XML解析最简单的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找某种属性的XML文件。例如,包含以下模式的文件:

I'm searching for xml files that have certain properties. For example, files that contain the following pattern:

<param-value>
  <name>Hosts</name>
  <description>some description</description>
  <value></value>
</param-value>

有关这样的文件,我想分析的另一个变量的值,如:

For such files, I'd like to parse the value of another tag, such as:

<param-value>
  <name>Roles</name>
  <description>some description</description>
  <value>asdf</value>
</param-value>

和打印出来的文件名以ASDF一起。什么是命令行实现这一点最简单的方法?

And print out the file name along with "asdf". What's the simplest way to accomplish this from the command line?

一种方法我想只是使用grep的-l选项匹配的文件过滤掉,然后使用的xargs grep来提取角色的价值。然而,grep的不能很好与多行正则表达式工作。我看到了另一个问题,那表明它可能与-Pzo选项来完成,但没有任何运气得到它在我的情况下工作。有没有更简单的方法呢?

One approach I was thinking of was just using grep with the -l option to filter the matching files out, and then using xargs grep to extract the value of Roles. However, grep doesn't work well with multi-line regexes. I saw another question that showed it could be done with the -Pzo options, but didn't have any luck getting it to work in my case. Is there a simpler approach?

推荐答案

下面的Linux命令使用XPath在XML文件中访问指定的值

The following linux command uses XPath to access specified values within the XML file

for xml in `find . -name "*.xml"`
do  
echo $xml `xmllint --xpath "/param-value/value/text()" $xml`| awk 'NF>1'
done

示例输出匹配的XML文件:

Example output for matching XML files:

./test1.xml asdf
./test4.xml 1234

这篇关于做到从UNIX命令行基本的XML解析最简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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