【发布时间】:2021-11-26 22:09:45
【问题描述】:
我想使用 sed 从包含的 XML 字符串中提取 IP4 和 IPv6 地址。不幸的是,该脚本将在使用busybox(OpenWRT)的系统上运行。因此,我无法访问可以使事情变得更容易的完整 grep。我正在努力为 sed 提供正确的字符串,专门用于提取第一个 XML 和第二个 XML 之间的测试。有人可以帮忙吗?
<?xml version="1.0" encoding="utf-8"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <s:Body> <u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"> <NewExternalIPAddress>12.116.116.39</NewExternalIPAddress> </u:GetExternalIPAddressResponse> </s:Body> </s:Envelope>
<?xml version="1.0" encoding="utf-8"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <s:Body> <u:X_AVM_DE_GetExternalIPv6AddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"> <NewExternalIPv6Address>2341:9e8:601b:797d:cece:1eff:fedb:75f2</NewExternalIPv6Address> <NewPrefixLength>64</NewPrefixLength> <NewValidLifetime>258993</NewValidLifetime> <NewPreferedLifetime>172593</NewPreferedLifetime> </u:X_AVM_DE_GetExternalIPv6AddressResponse> </s:Body> </s:Envelope>
【问题讨论】:
-
Don't Parse XML/HTML With Regex. 我建议使用 XML/HTML 解析器 (xmlstarlet, xmllint ...)。