Text-Processing
XML 數據到 CSV
需要一些快速幫助,因為我無法獲得所需的輸出:
2020-05-19 19:03:07,135 INFO [Container : 8504] [HttpUtil.java]requestXML: <?xml version="1.0"? ><COMMAND><TYPE>RCTRFREQ</TYPE><DN1>99847</DN1><AMOUNT>49</AMOUNT></COMMAND> - 2020-05-19 19:05:07,135 INFO [Container : 8504] [HttpUtil.java]requestXML: <?xml version="1.0"? ><COMMAND><PE>RC</PE><DN1>92847</DN1><AMOUNT>19</AMOUNT></COMMAND> - 2020-05-19 19:05:07,135 INFO [Container : 8504] [HttpUtil.java]requestXML: <?xml version="1.0"? ><COMMAND><DN1>947</DN1><TYPE>RC</TYPE><AMOUNT>29</AMOUNT></COMMAND> -
所需輸出:
Time,DN1,AMOUNT 2020-05-19 19:03:07,99847,49 2020-05-19 19:05:07,92847,19 2020-05-19 19:05:07,947,29
$ awk ' BEGIN { FS=",|</?(DN1|AMOUNT)>"; OFS=","; print "Time", "DN1", "AMOUNT" } { print $1, $3, $(NF-1) } ' file Time,DN1,AMOUNT 2020-05-19 19:03:07,99847,49 2020-05-19 19:05:07,92847,19 2020-05-19 19:05:07,947,29
上面告訴 awk 將輸入分隔為由與儲存在其中的正則表達式匹配的字元串分隔的欄位
FS
(即,
,<DN1>
,</DN1>
,<AMOUNT>
和</AMOUNT>
),然後列印第一個、第三個和倒數第二個這樣的欄位。以下是上述如何將每條記錄拆分為欄位:
$ awk -F',|</?(DN1|AMOUNT)>' '{print "----" ORS $0; for (i=1;i<=NF;i++) print NR, i "/" NF, $i}' file ---- 2020-05-19 19:03:07,135 INFO [Container : 8504] [HttpUtil.java]requestXML: <?xml version="1.0"? ><COMMAND><TYPE>RCTRFREQ</TYPE><DN1>99847</DN1><AMOUNT>49</AMOUNT></COMMAND> - 1 1/6 2020-05-19 19:03:07 1 2/6 135 INFO [Container : 8504] [HttpUtil.java]requestXML: <?xml version="1.0"? ><COMMAND><TYPE>RCTRFREQ</TYPE> 1 3/6 99847 1 4/6 1 5/6 49 1 6/6 </COMMAND> - ---- 2020-05-19 19:05:07,135 INFO [Container : 8504] [HttpUtil.java]requestXML: <?xml version="1.0"? ><COMMAND><PE>RC</PE><DN1>92847</DN1><AMOUNT>19</AMOUNT></COMMAND> - 2 1/6 2020-05-19 19:05:07 2 2/6 135 INFO [Container : 8504] [HttpUtil.java]requestXML: <?xml version="1.0"? ><COMMAND><PE>RC</PE> 2 3/6 92847 2 4/6 2 5/6 19 2 6/6 </COMMAND> - ---- 2020-05-19 19:05:07,135 INFO [Container : 8504] [HttpUtil.java]requestXML: <?xml version="1.0"? ><COMMAND><DN1>947</DN1><TYPE>RC</TYPE><AMOUNT>29</AMOUNT></COMMAND> - 3 1/6 2020-05-19 19:05:07 3 2/6 135 INFO [Container : 8504] [HttpUtil.java]requestXML: <?xml version="1.0"? ><COMMAND> 3 3/6 947 3 4/6 <TYPE>RC</TYPE> 3 5/6 29 3 6/6 </COMMAND> -