2016-05-18 4 views
0

与えられたxmlの指定されたタグからunixを使って値を抽出したいと思います。私はフォーマットされていないXML(1行にすべてのデータ)を持っていると私はPolNumberタグを検索する必要があります。同じ行に複数回存在します。grepを使わずにUnixでXMLからデータを抽出

親切

grep -oP "<PolNumber>[0-9]*</PolNumber>" samp.xml | grep -oe '\([0-9]*\)' 

以下grepコマンドを使用していますが、オンラインのUNIXコンパイルウェブサイトでの作業が、同じことが、私には動作しないことで期待どおりに働いている

<?xml version="1.0" encoding="UTF-8"?><TXLife><UserAuthRequest><UserLoginName>FirstPenn</UserLoginName><UserPswd><CryptType>None</CryptType><Pswd>None</Pswd></UserPswd><UserDate>2016-05-06</UserDate><UserTime>11:06</UserTime><VendorApp><VendorName VendorCode="FPPTB">FirstPenn</VendorName><AppName>ACORD XML Download</AppName><AppVer>1.0</AppVer></VendorApp></UserAuthRequest><TXLifeRequest><TransRefGUID>4B6BB6FB-6FA0-4678-A3A2-862E7AE7D884</TransRefGUID><TransType tc="1125"/><TransExeDate>2016-05-06</TransExeDate><TransExeTime>11:06</TransExeTime><TransMode tc="2"/><InquiryLevel tc="3"/><MaxRecords>0</MaxRecords><PendingResponseOK tc="0">False</PendingResponseOK><NoResponseOK tc="1">True</NoResponseOK><TestIndicator tc="0">False</TestIndicator><OLifE Version="2.7"><SourceInfo><CreationDate>2016-05-06</CreationDate><SourceInfoName>First Penn-Pacific</SourceInfoName><SourceInfoDescription>Pending Case Status</SourceInfoDescription><FileControlID>1223232304</FileControlID></SourceInfo></Holding><Holding id="HLD_4902160"><HoldingTypeCode tc="2"/><HoldingStatus tc="4"/><AsOfDate>2016-05-05</AsOfDate><Policy CarrierPartyID="LLCTB_4902160"><CarrierCode>LLCTB</CarrierCode><PolNumber>4902160</PolNumber><LineOfBusiness tc="1">Life</LineOfBusiness><ProductType tc="4"/><ProductCode>VLON14  </ProductCode><PlanName>VLON14  </PlanName><PolicyStatus tc="24">Approved, not issued</PolicyStatus><Jurisdiction tc="56"/><EffDate>2016-02-18</EffDate><PaymentMode tc="9">Single Payment</PaymentMode><PaymentAmt>62336.0000</PaymentAmt><Life><TargetPremAmt>5759.9700</TargetPremAmt><TotalRolloverAmt>0.0000</TotalRolloverAmt><FaceAmt>261579.0000</FaceAmt><Coverage id="COV_4902160_1"><IndicatorCode tc="1"/><LivesType tc="2147483647"/><LifeParticipant PartyID="INS_4902160_1"><LifeParticipantRoleCode tc="1"/><IssueAge>53</IssueAge><IssueGender tc="1"/><TobaccoPremiumBasis tc="1">Non Smoker</TobaccoPremiumBasis><PermTableRating tc="1"/><UnderwritingClass tc="2">Preferred risk</UnderwritingClass></LifeParticipant></Coverage></Life><Holding id="HLD_4902270"><HoldingTypeCode tc="2"/><HoldingStatus tc="4"/><AsOfDate>2016-05-06</AsOfDate><Policy CarrierPartyID="LLCTB_4902270"><CarrierCode>LLCTB</CarrierCode><PolNumber>4902270</PolNumber><LineOfBusiness tc="1">Life</LineOfBusiness><ProductType tc="4"/><ProductCode>VLON14  </ProductCode><PlanName>VLON14  </PlanName><PolicyStatus tc="8">Pending Issue</PolicyStatus><Jurisdiction tc="17"/><EffDate>2016-02-24</EffDate><PaymentMode tc="1">Annual</PaymentMode><PaymentAmt>2532.0000</PaymentAmt><Life><TargetPremAmt>7422.0000</TargetPremAmt><TotalRolloverAmt>0.0000</TotalRolloverAmt><FaceAmt>200000.0000</FaceAmt><Coverage id="COV_4902270_1"><IndicatorCode tc="1"/><LivesType tc="2147483647"/><LifeParticipant PartyID="INS_4902270_1"><LifeParticipantRoleCode tc="1"/><IssueAge>69</IssueAge><IssueGender tc="2"/><TobaccoPremiumBasis tc="1">Non Smoker</TobaccoPremiumBasis><PermTableRating tc="1"/><UnderwritingClass tc="1">Standard Risk</UnderwritingClass></LifeParticipant></Coverage></Life> 

以下のxmlを見つけます機械。それはグレープ無効なオプション - oと言います。私はバージョンの問題や何かについてはわかりませんが、私は現在のunixで修正する必要があります。私はそれを手伝ってください。 grepを、セッドまたは任意のような非XML-パーサと事前に

おかげ
マニバンナン

+0

のUnixのバージョン/味は何ですか?お使いのOSのgrep manページで同様のオプションを見つけようとしましたか? – ZnArK

+0

あなたは 'grep'だけを使って解決策を期待していますか? awkはサポートされていますか? – Inian

+0

ありがとうございます。 – Mani

答えて

1

使用して、簡単なユーティリティ:

tr "<" "\n" < samp.xml | grep "^PolNumber" | cut -d">" -f2 
+0

その完璧な。期待どおりに働く。ありがとうございました@Walter A – Mani

0

解析XMLは通常、悪い考えです。

とにかく、ここにsedを持つ迅速かつ汚いソリューションです:

sed 's#\(<PolNumber>[0-9]*\)</PolNumber>#\1\n#g' samp.xml | grep '<PolNumber>' | sed 's#.*<PolNumber>\([0-9]*\)$#\1#' 

あなたのXMLが1行である場合にのみ動作します。

+0

ありがとうございました。 – Mani

関連する問題