gpt4 book ai didi

xml - 在 xml 字符串上查找特定标签

转载 作者:太空宇宙 更新时间:2023-11-04 05:26:44 25 4
gpt4 key购买 nike

我有一个很大的事务日志文件。下面是 XML 响应字符串(尤其是在 erricson IN 日志上)。我只想要响应代码和这个值。目前,当我在 Linux 上 grep 时,所有完整的字符串都会出现。

下面是我的日志条目:

2015-05-18 13:01:33,817 UserMsisdn=919813290710,:: CustomerMsisdn=919671055440Stage is TOP:: After reading complete data from IN::<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><struct><member><name>accountAfterRefill</name><value><struct><member><name>accountFlags</name><value><struct><member><name>activationStatusFlag</name><value><boolean>1</boolean></value></member><member><name>negativeBarringStatusFlag</name><value><boolean>0</boolean></value></member><member><name>serviceFeePeriodExpiryFlag</name><value><boolean>0</boolean></value></member><member><name>serviceFeePeriodWarningActiveFlag</name><value><boolean>0</boolean></value></member><member><name>supervisionPeriodExpiryFlag</name><value><boolean>0</boolean></value></member><member><name>supervisionPeriodWarningActiveFlag</name><value><boolean>0</boolean></value></member></struct></value></member><member><name>accountValue1</name><value><string>9058</string></value></member><member><name>offerInformationList</name><value><array><data><value><struct><member><name>expiryDate</name><value><dateTime.iso8601>20150520T12:00:00+0000</dateTime.iso8601></value></member><member><name>offerID</name><value><i4>2220</i4></value></member><member><name>offerType</name><value><i4>0</i4></value></member><member><name>startDate</name><value><dateTime.iso8601>20150505T12:00:00+0000</dateTime.iso8601></value></member></struct></value><value><struct><member><name>expiryDate</name><value><dateTime.iso8601>99991231T12:00:00+0000</dateTime.iso8601></value></member><member><name>offerID</name><value><i4>2692</i4></value></member><member><name>offerType</name><value><i4>0</i4></value></member><member><name>startDate</name><value><dateTime.iso8601>20100226T12:00:00+0000</dateTime.iso8601></value></member></struct></value></data></array></value></member><member><name>serviceClassCurrent</name><value><i4>2742</i4></value></member><member><name>serviceOfferings</name><value><array><data><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>1</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>2</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>3</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>4</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>5</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>6</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>7</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>8</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>9</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>10</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>11</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>12</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>13</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>14</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>15</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>16</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>17</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>18</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>19</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>20</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>21</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>22</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>23</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>24</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>25</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>26</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>27</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>28</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>29</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>30</i4></value></member></struct></value><value><struct><member><name>serviceOfferingActiveFlag</name><value><boolean>0</boolean></value></member><member><name>serviceOfferingID</name><value><i4>31</i4></value></member></struct></value></data></array></value></member><member><name>usageAccumulatorInformation</name><value><array><data><value><struct><member><name>accumulatorID</name><value><i4>2212</i4></value></member><member><name>accumulatorValue</name><value><i4>0</i4></value></member></struct></value><value><struct><member><name>accumulatorID</name><value><i4>2253</i4></value></member><member><name>accumulatorValue</name><value><i4>0</i4></value></member></struct></value></data></array></value></member></struct></value></member><member><name>availableServerCapabilities</name><value><array><data><value><i4>537207364</i4></value></data></array></value></member><member><name>currency1</name><value><string>INR</string></value></member><member><name>languageIDCurrent</name><value><i4>1</i4></value></member><member><name>masterAccountNumber</name><value><string>9671055440</string></value></member><member><name>negotiatedCapabilities</name><value><array><data><value><i4>0</i4></value></data></array></value></member><member><name>originTransactionID</name><value><string>HR1573404963</string></value></member><member><name>refillType</name><value><i4>3</i4></value></member><member><name>**responseCode</**name><value**><i4>0</i4></**value></member><member><name>segmentationID</name><value><string>N200</string></value></member><member><name>transactionAmount</name><value><string>8600</string></value></member><member><name>transactionCurrency</name><value><string>INR</string></value></member></struct></value></param></params></methodResponse>

下面是我为此使用的命令,但它无法正常工作:

grep 'responseCode' TransactionLog.log.21_05_2015-12|egrep "<member>.*</member>"

预期输出为:

responseCode</name><value><i4>0</i4>

最佳答案

日志条目是很长的 XML 行。 grep 命令仅查找包含匹配模式的整行。 grep 命令找到了匹配的行并将其输出 - 只是它是一个很长的行。

您只想提取已找到的行的一部分。最好使用其他工具来完成此操作,例如 perlawk。您还可以使用 sed 在成员标记处将单行切成多行,然后 grep 查找您想要的特定项目。

使用 sed 添加换行符有点尴尬,但在这个问题/答案中对此进行了解释:How to insert a newline in front of a pattern?

要在每个 子句处换行,您可以这样写:

sed -e 's/<member>/\'$'\n<member>/g'

然后您可以仅选择 ResponseCode 部分,给出:

grep responseCode Transaction.log | sed -e 's/<member>/\'$'\n<member>/g' | grep responseCode

现在生成的输出是:

<member><name>**responseCode</**name><value**><i4>0</i4></**value></member>

您现在可以使用 sed 将其进一步削减:

grep responseCode Transaction.log | sed -e 's/<member>/\'$'\n<member>/g' | grep responseCode | \
sed -e 's/<member><name>//g' -e 's/<\/name>//g' -e 's/<\/value>//g' -e 's/<\/member>//g'

..现在获得您真正想要的格式应该很简单。

关于xml - 在 xml 字符串上查找特定标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30367520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com