来源: http://bbs.chinaunix.net/thread-4216848-1-1.html
- <?xml version="1.0" encoding="UTF-8"?>
- <issues type="array">
- <issue>
- <id>4947</id>
- <project name="你好" id="2"/>
- <priority name="高优先级" id="4"/>
- <assigned_to name="张三" id="1215"/>
- <created_on>Thu Dec 24 16:33:00 +0800 2015</created_on>
- <updated_on>Thu Dec 24 16:38:13 +0800 2015</updated_on>
- </issue>
- <issue>
- <id>4948</id>
- <project name="我好" id="2"/>
- <priority name="低优先级" id="4"/>
- <assigned_to name="李四" id="1215"/>
- <created_on>Thu Dec 25 16:33:00 +0800 2015</created_on>
- <updated_on>Thu Dec 25 16:38:13 +0800 2015</updated_on>
- </issue>
- <issue>
- <id>4949</id>
- <project name="大家好" id="2"/>
- <priority name="高优先级" id="4"/>
- <assigned_to name="王麻子" id="1215"/>
- <created_on>Thu Dec 26 16:33:00 +0800 2015</created_on>
- <updated_on>Thu Dec 26 16:38:13 +0800 2015</updated_on>
- </issue>
- </issues>
比如:- <?xml version="1.0" encoding="UTF-8"?>
- <issues type="array">
- <issue>
- <id>4947</id>
- <project name="你好" id="2"/>
- <priority name="高优先级" id="4"/>
- <assigned_to name="张三" id="1215"/>
- <created_on>Thu Dec 24 16:33:00 +0800 2015</created_on>
- <updated_on>Thu Dec 24 16:38:13 +0800 2015</updated_on>
- </issue>
- <issue>
- <id>4948</id>
- <project name="我好" id="2"/>
- <priority name="低优先级" id="4"/>
- <assigned_to name="李四" id="1215"/>
- <created_on>Thu Dec 25 16:33:00 +0800 2015</created_on>
- <updated_on>Thu Dec 25 16:38:13 +0800 2015</updated_on>
- </issue>
- <issue>
- <id>4949</id>
- <project name="大家好" id="2"/>
- <priority name="高优先级" id="4"/>
- <assigned_to name="王麻子" id="1215"/>
- <created_on>Thu Dec 26 16:33:00 +0800 2015</created_on>
- <updated_on>Thu Dec 26 16:38:13 +0800 2015</updated_on>
- </issue>
- </issues>
复制代码 我期望提取出,如何实现呢:- 4947 你好 张三
- 4948 我好 李四
- 4949 大家好 王麻子
复制代码 |
方法三:
xmlstarlet sel -t -m "//issue" -c "concat(./id/text(), ' ', ./project/@name, ' ', ./assigned_to/@name)" -n file.xml
方法二:
- sed -nr '/<issue>/,/<\/issue>/{H;//{x;s#.*<id>([^<]*).*<project name="([^"]*).*<assigned_to name="([^"]*).*#\1 \2 \3#p}}' file
- 4947 你好 张三
- 4948 我好 李四
- 4949 大家好 王麻子
复制代码
- awk -F'[<>"]' '/id>|project|assigned/{printf ++c%3 ? $3" " : $3"\n"}' b
- 4947 你好 张三
- 4948 我好 李四
- 4949 大家好 王麻子
复制代码