我想读取包含所有日志和一些xml节点之间的日志文件。
input.log
logs....
logs....
This is Sample File with lots of logs and now somewhere there is start xml tag <Start> start1
<child1>12345</child1>
<child2>67890</child2>
</Start>. Now writing 2nd start and end tags <Start> start2
<child1>54321</child1>
<child2>09876</child2>
</Start> some more logs...
logs...
logs...
logs... some other xml nodes
logs...
我想让shell脚本在
下面打印<Start> start1
<child1>12345</child1>
<child2>67890</child2>
</Start>
<Start> start2
<child1>54321</child1>
<child2>09876</child2>
</Start>
我跟随下面的链接,并能够得到如下的结果链接:https://www.unix.com/shell-programming-and-scripting/119502-extracting-text-between-two-unique-lines.html
This is Sample File with start and end tags <Start> start1
<child1>12345</child1>
<child2>67890</child2>
</Start>. Now writing 2nd start and end tags <Start> start2
<child1>54321</child1>
<child2>09876</child2>
</Start> Ending the file content.
这是因为它正在搜索并打印所有行,直到找到。因此出现了前后文本。
我也尝试使用grep命令,但无法找出。使用的Grep命令:echo "$value" | grep -oP '(?<=<Start>).*?(?=</Start>)'
如果要解析XML文件,请使用XML解析器。Xmlstarlet是一个:
xmlstarlet sel -t -m //Start -c . -n file.xml
给定的file.xml包含
<root>
This is Sample File with start and end tags <Start> start1
<child1>12345</child1>
<child2>67890</child2>
</Start>. Now writing 2nd start and end tags <Start> start2
<child1>54321</child1>
<child2>09876</child2>
</Start> Ending the file content.
</root>
则xmlstarlet
命令输出
<Start> start1
<child1>12345</child1>
<child2>67890</child2>
</Start>
<Start> start2
<child1>54321</child1>
<child2>09876</child2>
</Start>