如何从Unix命令行中删除XML标签? [英] How to remove XML tags from Unix command line?

查看:100
本文介绍了如何从Unix命令行中删除XML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓紧一个XML文件,该文件的输出如下:

I am grepping an XML File, which gives me output like this:

<tag>data</tag>
<tag>more data</tag>
...

注意,这是一个平面文件,而不是XML树。我想删除XML标记,只显示它们之间的数据。我正在从命令行执行所有这些操作,并且想知道是否有比将其两次管道传输到awk更好的方法...

Note, this is a flat file, not an XML tree. I want to remove the XML tags and just display the data in between. I'm doing all this from the command line and was wondering if there is a better way than piping it into awk twice...

cat file.xml | awk -F'>' '{print $2}' | awk -F'<' '{print $1}'

理想情况下,我想这样做

Ideally, I would like to do this in one command

推荐答案

如果您的文件看起来像这样,则 sed 可以帮助您:

If your file looks just like that, then sed can help you:

sed -e 's/<[^>]*>//g' file.xml

当然您不应使用正则表达式来解析XML 因为很难

这篇关于如何从Unix命令行中删除XML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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