我有以下 XML 代碼,我必須在其中獲取 RFCnumber 值并將其分配給變數。例如:從下面的代碼中,我必須獲取1200021992RFCNumber 屬性名稱并將其分配給一個變數。
<?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object Type="System.Management.Automation.PSCustomObject">
<Property Name="d" Type="System.Management.Automation.PSCustomObject">
<Property Name="__metadata" Type="System.Management.Automation.PSCustomObject">
<Property Name="id" Type="System.String">https://example.com</Property>
<Property Name="uri" Type="System.String">https://example.com</Property>
<Property Name="LongDescription" Type="System.String">Test???Description???of???Change</Property>
<Property Name="OperationType" Type="System.String">new</Property>
<Property Name="ChangeType" Type="System.String">ZMCR</Property>
<Property Name="Status" Type="System.String">Success</Property>
<Property Name="ShortDescription" Type="System.String">Test???Short???Description</Property>
<Property Name="CycleTypeId" Type="System.String">8200000083</Property>
<Property Name="RfcNumber" Type="System.String">1200021992</Property>
我需要一些幫助。我正在嘗試以下代碼,但它不起作用:
$rfc = ($response -split "<d:RfcNumber>|</d:RfcNumber>")[X]
echo $rfc
uj5u.com熱心網友回復:
第一種解決方案:使用您顯示的示例,請嘗試以下awk代碼。
awk -F'[><]'
'/<Property Name="RfcNumber" Type="System\.String">/{
print $3
}
' Input_file
或者,如果您只有 1 個RefNumber并且您只想列印 1 個值,那么最好exit從命令中保存某個時間,在這種情況下,您可以嘗試以下awk代碼,對上面的形式進行一些調整:
awk -F'[><]'
'/<Property Name="RfcNumber" Type="System\.String">/{
print $3
exit
}
' Input_file
說明:簡單的解釋是,在程式的主塊中-F(欄位分隔符)為>and然后列印 OP 需要輸出的第三個欄位。<<Property Name="RfcNumber" Type="System.String"
第二種解決方案:使用sed您可以嘗試以下解決方案一次。
sed -n '/<Property Name="RfcNumber" Type="System\.String"/{s/.*">//;s/<\/.*//p}' Input_file
或者,如果您在整個檔案中只有 1 個匹配值,那么最好從程式中退出/退出,而不是在這種情況下讀取整個 Input_file,請嘗試以下操作:
sed -n '/<Property Name="RfcNumber" Type="System\.String"/{s/.*">//;s/<\/.*//p;q}' Input_file
uj5u.com熱心網友回復:
使用sed
$ rfc=$(sed -En '/RfcNumber/s/[^>]*>([^<]*).*/\1/p' input_file)
$ echo "$rfc"
1200021992
uj5u.com熱心網友回復:
為什么不在 powershell 中使用內置的 xml 決議器?例如:
[xml] $x = gc infile.xml
$x.objects.object.property | ? name -like "rfcnumber" | select -exp "#text"
輸出:
1200021992
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515934.html
標籤:电源外壳壳awksed
