简体   繁体   English

使用脚本提取文本

[英]Extracting text with script

We have an xml file of the following format: 我们有以下格式的xml文件:

<mailBox>
...
</mailBox>
<mailBox>demon</mailBox>
<tz>16385</tz>
<Contact>
....
</Contact>
</mailBox>
<mailBox>
...
</mailBox>

Is there a way to extract a particular node out of this xml using a sed/awk/grep one-liner? 有没有一种方法可以使用sed / awk / grep单线从此xml中提取特定节点?
I was looking for somethign in the format 我正在寻找某种格式的东西

`sed -n 'mailBox\>demon,......p`

on your sample the tag demon is on the same line and not in rest of file. 在您的样本中,标记恶魔位于同一行,而不在文件的其余部分。 Error or specific 错误或特定

If it's an error (so mailbox tag are on separate lines) 如果是错误(因此邮箱标记在单独的行上)

sed -n '1h;1!H;${x
s/.*\(<mailBox>demon.*\)/\1/;s|</mailBox>.*||;p
}' YourFile

您可以尝试:

perl -0777 -nE 'foreach (/<mailBox>(.*?)<\/mailBox>/sg) {say $_}' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM