我有一个很大的事务日志文件。在那下面是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>
答案 0 :(得分:0)
日志条目是非常长的XML行。 grep
命令仅查找包含匹配模式的整行。 grep
命令找到了匹配的行并为您输出 - 只是它是一个非常长的行。
您想要只提取它找到的行的一部分。最好使用其他工具,例如perl
或awk
。您还可以使用sed
将单行切成成员标记的多行,然后grep查找您想要的特定项目。
使用sed来添加换行符有点尴尬,但在这个问题/答案中对此进行了解释:How to insert a newline in front of a pattern?
在每个&lt; member&gt;处打破这一行您可以写这个条款:
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'
..现在获得你真正想要的格式应该是微不足道的。