Splunk 查询不 return 字段值的多个实例
Splunk query does not return multiple instances of the field values
我有一个 Splunk 查询,它识别从 xml 文件中提取的所有字段,但不是所有数据实例。
有一个巨大的 xml 文件,其中包含多个结果实例,但在字段列表中仅标识 1 个结果。
这是查询:
source="service.log" sourcetype="dispatchapp" "<billingMethod>RULE</billingMethod>" "createMessage MsgSource" "<purchCostReference>2618252</purchCostReference>" "<eventType>DISPATCH</eventType>" | xmlkv
这是结果:
突出显示的字段是我感兴趣的字段。有 3 个运营商标签和许多 ns:numberCodes。但它只显示 1。
下面是有数据
的xml
<purchasedCost>
<purchasedCostTripSegment>
<purchCostReference>2618252</purchCostReference>
<carrier>NS</carrier>
<vendorType>RAIL</vendorType>
<carrierTrailerType>53RP</carrierTrailerType>
<origin>
<ns2:numberCode>4061</ns2:numberCode>
<ns2:locAbbr>NSAU</ns2:locAbbr>
<ns2:address1>6300 SOUTH INDIANA AVENUE</ns2:address1>
<ns2:city>CHICAGO</ns2:city>
<ns2:stateProvince>IL</ns2:stateProvince>
<ns2:postalCode>60637</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>CST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</origin>
<destination>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</destination>
<stopOff>
<ns2:stopOffLocation>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</ns2:stopOffLocation>
</stopOff>
<schedDispatchDate>2020-05-20T05:00:00.000Z</schedDispatchDate>
<actualDispatchDate>2020-05-21T08:20:53.000Z</actualDispatchDate>
<estimatedArrivalDate>2020-05-25T08:18:53.000Z</estimatedArrivalDate>
<billingMethod>LOCAL</billingMethod>
<STCCCode>4711110</STCCCode>
<planNumber>065</planNumber>
<powerType>1X</powerType>
<powerOnlyFlag>false</powerOnlyFlag>
</purchasedCostTripSegment>
<purchasedCostTripSegment>
<purchCostReference>2618252</purchCostReference>
<carrier>BNSF</carrier>
<vendorType>RAIL</vendorType>
<carrierTrailerType>53RP</carrierTrailerType>
<origin>
<ns2:numberCode>4022</ns2:numberCode>
<ns2:locAbbr>BNSF</ns2:locAbbr>
<ns2:address1>3770 EAST WASHINGTON AVENUE</ns2:address1>
<ns2:city>LOS ANGELES</ns2:city>
<ns2:stateProvince>CA</ns2:stateProvince>
<ns2:postalCode>90040</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>PST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</origin>
<destination>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</destination>
<stopOff>
<ns2:stopOffLocation>
<ns2:numberCode>9996</ns2:numberCode>
<ns2:stateProvince>DU</ns2:stateProvince>
<ns2:postalCode>00000</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>1</ns2:numberType>
</ns2:stopOffLocation>
</stopOff>
<schedDispatchDate>2020-05-20T05:00:00.000Z</schedDispatchDate>
<actualDispatchDate>2020-05-21T08:20:53.000Z</actualDispatchDate>
<estimatedArrivalDate>2020-05-25T08:19:53.000Z</estimatedArrivalDate>
<billingMethod>RULE</billingMethod>
<STCCCode>4711110</STCCCode>
<planNumber>065</planNumber>
<powerType>1X</powerType>
<powerOnlyFlag>false</powerOnlyFlag>
</purchasedCostTripSegment>
<purchasedCostTripSegment>
<purchCostReference>2618252</purchCostReference>
<carrier>NS</carrier>
<vendorType>RAIL</vendorType>
<carrierTrailerType>53RP</carrierTrailerType>
<origin>
<ns2:numberCode>4061</ns2:numberCode>
<ns2:locAbbr>NSAU</ns2:locAbbr>
<ns2:address1>6300 SOUTH INDIANA AVENUE</ns2:address1>
<ns2:city>CHICAGO</ns2:city>
<ns2:stateProvince>IL</ns2:stateProvince>
<ns2:postalCode>60637</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>CST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</origin>
<destination>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</destination>
<stopOff>
<ns2:stopOffLocation>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</ns2:stopOffLocation>
</stopOff>
<schedDispatchDate>2020-05-20T05:00:00.000Z</schedDispatchDate>
<actualDispatchDate>2020-05-21T08:20:53.000Z</actualDispatchDate>
<estimatedArrivalDate>2020-05-25T08:20:53.000Z</estimatedArrivalDate>
<billingMethod>LOCAL</billingMethod>
<STCCCode>4711110</STCCCode>
<planNumber>065</planNumber>
<powerType>1X</powerType>
<powerOnlyFlag>false</powerOnlyFlag>
</purchasedCostTripSegment>
</purchasedCost>
如何提取每个采购成本段的 numberCode 和 carrier 实例?
这就是我如何在 xml 文件中获取一个值的多个实例...
您可以使用 max_match
选项告诉 rex
到 return 他们两个。
| rex max_match=0 "\<ns2\:numberCode\>(?P<origin>[^\<]+)"
| eval originCode = mvindex(origin, 0), destCode = mvindex(origin, 1)
不幸的是,无法知道匹配的顺序,因此如果 XML 中的目的地先于起点,则此操作失败。
你应该可以使用spath
提取整个XML并参考"origin.numberCode"和"destination.numberCode",但我没有足够的经验命令提供很多指导。
我有一个 Splunk 查询,它识别从 xml 文件中提取的所有字段,但不是所有数据实例。 有一个巨大的 xml 文件,其中包含多个结果实例,但在字段列表中仅标识 1 个结果。 这是查询:
source="service.log" sourcetype="dispatchapp" "<billingMethod>RULE</billingMethod>" "createMessage MsgSource" "<purchCostReference>2618252</purchCostReference>" "<eventType>DISPATCH</eventType>" | xmlkv
这是结果:
突出显示的字段是我感兴趣的字段。有 3 个运营商标签和许多 ns:numberCodes。但它只显示 1。 下面是有数据
的xml<purchasedCost>
<purchasedCostTripSegment>
<purchCostReference>2618252</purchCostReference>
<carrier>NS</carrier>
<vendorType>RAIL</vendorType>
<carrierTrailerType>53RP</carrierTrailerType>
<origin>
<ns2:numberCode>4061</ns2:numberCode>
<ns2:locAbbr>NSAU</ns2:locAbbr>
<ns2:address1>6300 SOUTH INDIANA AVENUE</ns2:address1>
<ns2:city>CHICAGO</ns2:city>
<ns2:stateProvince>IL</ns2:stateProvince>
<ns2:postalCode>60637</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>CST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</origin>
<destination>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</destination>
<stopOff>
<ns2:stopOffLocation>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</ns2:stopOffLocation>
</stopOff>
<schedDispatchDate>2020-05-20T05:00:00.000Z</schedDispatchDate>
<actualDispatchDate>2020-05-21T08:20:53.000Z</actualDispatchDate>
<estimatedArrivalDate>2020-05-25T08:18:53.000Z</estimatedArrivalDate>
<billingMethod>LOCAL</billingMethod>
<STCCCode>4711110</STCCCode>
<planNumber>065</planNumber>
<powerType>1X</powerType>
<powerOnlyFlag>false</powerOnlyFlag>
</purchasedCostTripSegment>
<purchasedCostTripSegment>
<purchCostReference>2618252</purchCostReference>
<carrier>BNSF</carrier>
<vendorType>RAIL</vendorType>
<carrierTrailerType>53RP</carrierTrailerType>
<origin>
<ns2:numberCode>4022</ns2:numberCode>
<ns2:locAbbr>BNSF</ns2:locAbbr>
<ns2:address1>3770 EAST WASHINGTON AVENUE</ns2:address1>
<ns2:city>LOS ANGELES</ns2:city>
<ns2:stateProvince>CA</ns2:stateProvince>
<ns2:postalCode>90040</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>PST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</origin>
<destination>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</destination>
<stopOff>
<ns2:stopOffLocation>
<ns2:numberCode>9996</ns2:numberCode>
<ns2:stateProvince>DU</ns2:stateProvince>
<ns2:postalCode>00000</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>1</ns2:numberType>
</ns2:stopOffLocation>
</stopOff>
<schedDispatchDate>2020-05-20T05:00:00.000Z</schedDispatchDate>
<actualDispatchDate>2020-05-21T08:20:53.000Z</actualDispatchDate>
<estimatedArrivalDate>2020-05-25T08:19:53.000Z</estimatedArrivalDate>
<billingMethod>RULE</billingMethod>
<STCCCode>4711110</STCCCode>
<planNumber>065</planNumber>
<powerType>1X</powerType>
<powerOnlyFlag>false</powerOnlyFlag>
</purchasedCostTripSegment>
<purchasedCostTripSegment>
<purchCostReference>2618252</purchCostReference>
<carrier>NS</carrier>
<vendorType>RAIL</vendorType>
<carrierTrailerType>53RP</carrierTrailerType>
<origin>
<ns2:numberCode>4061</ns2:numberCode>
<ns2:locAbbr>NSAU</ns2:locAbbr>
<ns2:address1>6300 SOUTH INDIANA AVENUE</ns2:address1>
<ns2:city>CHICAGO</ns2:city>
<ns2:stateProvince>IL</ns2:stateProvince>
<ns2:postalCode>60637</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>CST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</origin>
<destination>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</destination>
<stopOff>
<ns2:stopOffLocation>
<ns2:numberCode>4040</ns2:numberCode>
<ns2:locAbbr>CROX</ns2:locAbbr>
<ns2:address1>NORFOLK SOUTHERN RAILROAD</ns2:address1>
<ns2:address2>125 COUNTY ROAD</ns2:address2>
<ns2:city>CROXTON</ns2:city>
<ns2:stateProvince>NJ</ns2:stateProvince>
<ns2:postalCode>07307</ns2:postalCode>
<ns2:locType>FDEG</ns2:locType>
<ns2:numberType>8</ns2:numberType>
<ns2:timeZoneAbbr>EST</ns2:timeZoneAbbr>
<ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
</ns2:stopOffLocation>
</stopOff>
<schedDispatchDate>2020-05-20T05:00:00.000Z</schedDispatchDate>
<actualDispatchDate>2020-05-21T08:20:53.000Z</actualDispatchDate>
<estimatedArrivalDate>2020-05-25T08:20:53.000Z</estimatedArrivalDate>
<billingMethod>LOCAL</billingMethod>
<STCCCode>4711110</STCCCode>
<planNumber>065</planNumber>
<powerType>1X</powerType>
<powerOnlyFlag>false</powerOnlyFlag>
</purchasedCostTripSegment>
</purchasedCost>
如何提取每个采购成本段的 numberCode 和 carrier 实例?
这就是我如何在 xml 文件中获取一个值的多个实例...
您可以使用 max_match
选项告诉 rex
到 return 他们两个。
| rex max_match=0 "\<ns2\:numberCode\>(?P<origin>[^\<]+)"
| eval originCode = mvindex(origin, 0), destCode = mvindex(origin, 1)
不幸的是,无法知道匹配的顺序,因此如果 XML 中的目的地先于起点,则此操作失败。
你应该可以使用spath
提取整个XML并参考"origin.numberCode"和"destination.numberCode",但我没有足够的经验命令提供很多指导。