我有一個包含 html 回應代碼的檔案,如下所示:
<d:ChangeType>SSCR</d:ChangeType>
<d:Status>Success</d:Status>
<d:ShortDescription>API </d:ShortDescription>
<d:CycleTypeId>8000006005</d:CycleTypeId>
<d:RfcNumber>1200000910</d:RfcNumber>
<d:ExtrefNumber>API External</d:ExtrefNumber>
要求是獲取和之間的數字<d:RfcNumber>,</d:RfcNumber>即1200000910在本例中,并將其提供給變數。
我正在嘗試使用 sed,例如:
sed 's/1200000910.*//' test2.html
但它沒有為我提供預期的結果。
對此的任何幫助將不勝感激。
uj5u.com熱心網友回復:
第一個解決方案:由于您使用的是sed假設您在這里使用的是 shell。你可以awk在這里使用命令。只需使用和awk設定欄位分隔符作為所有行。在主程式中檢查欄位數是否大于 2,然后列印第二個欄位。<d:RfcNumber><\\/d:RfcNumber>
var=$(awk -F'<d:RfcNumber>|<\\/d:RfcNumber>' 'NF>2{print $2;exit}' Input_file)
第二種解決方案:在這里使用 GNUawk的match函式來獲取標簽之間的值。
var=$(awk 'match($0,/^<d:RfcNumber>([^<]*)<\/d:RfcNumber>/,arr){print arr[1];exit}' Input_file)
第三種解決方案:或者sed請嘗試以下代碼,使用-EGNU 選項sed在代碼中使用 ERE(擴展正則運算式)。
var=$(sed -E -n 's/^<d:RfcNumber>([^<]*)<\/d:RfcNumber>/\1/p' Input_file)
uj5u.com熱心網友回復:
...拆分方式:
#Read your file content
$response = (gc C:\tmp\testdata.txt)
$rfc = ($response -split "<d:RfcNumber>|</d:RfcNumber>")[5]
或正則運算式(但這可能會被優化):
#Read your file content
$response = (gc C:\tmp\testdata.txt)
($response | select-string "<d:RfcNumber>\d{10}").matches.groups.value -replace "<d:RfcNumber>"
或者您將其用作 xml:
[xml]$xml = '<root>' (($response -replace "d:") -join $null) '</root>'
$xml.root.RfcNumber
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/503773.html
上一篇:如何在PHP中顯示freadsocketunix的所有輸出?
下一篇:獲取gitrepo分支名稱串列
