输入文件:
<Response>
<Status>SUCCESS</Status>
<CustDtls>
<HeaderDtlsList>
<HeaderDtls>
<CustNbr>1001</CustNbr>
<SalesNbr>260236</SalesNbr>
<RowSequenceNbr>0</RowSequenceNbr>
<LineItemType>Header</LineItemType>
<CName>Alex</CName>
<CustomerType>PRO</CustomerType>
<DateOfBirth>1988-05-08</DateOfBirth>
<CostAmt>$110.9800</CostAmt>
</HeaderDtls>
<HeaderDtls>
<CustNbr>1001</CustNbr>
<SalesNbr>260239</SalesNbr>
<RowSequenceNbr>0</RowSequenceNbr>
<LineItemType>Header</LineItemType>
<CName>Seneta</CName>
<CustomerType>DIY</CustomerType>
<DateOfBirth>1900-01-01</DateOfBirth>
<CostAmt>$17.4000</CostAmt>
</HeaderDtls>
</HeaderDtlsList>
<ContactList>
<Contact>
<CustNbr>1001</CustNbr>
<SalesNbr>260243</SalesNbr>
<RowSequenceNbr>1</RowSequenceNbr>
<LineItemType>Contact</LineItemType>
<PhoneNumber>114-5099940</PhoneNumber>
<ExtensionNumber>123</ExtensionNumber>
</Contact>
<Contact>
<CustNbr>1001</CustNbr>
<SalesNbr>261942</SalesNbr>
<RowSequenceNbr>1</RowSequenceNbr>
<LineItemType>Contact</LineItemType>
<PhoneNumber>668-4958486</PhoneNumber>
<ExtensionNumber> </ExtensionNumber>
</Contact>
</ContactList>
</CustDtls>
</Response>
使用以下命令输出电流:
cat testxml.txt | perl -ne '/<\/HeaderDtls>/ && print "\n"; />(.*?)</ && !/<SalesNbr>/ && print $1."|"; /<SalesNbr>/ && /SalesNbr>?(.*?)<\/.*?(LineItemType>?)(.*)?<\/LineItemType/ && print $1."|".$3' > Header.txt
cat Header.txt
SUCCESS|1001|0|Header|Alex|PRO|1988-05-08|$110.9800|
1001|0|Header|Seneta|DIY|1900-01-01|$17.4000|
1001|1|Contact|114-5099940|123|1001|1|Contact|668-4958486| |
预期输出:
在单独的文件Header.txt
中需要标题详细信息,而不包含成功一词,且没有联系方式。
例如:
1001|0|Header|Alex|PRO|1988-05-08|$110.9800|
1001|0|Header|Seneta|DIY|1900-01-01|$17.4000|
还需要在单独的文件contacts.txt
1001|1|Contact|114-5099940|123|
1001|1|Contact|668-4958486| |
有人能解决这个问题吗?