Issue
I have following XML code where I have to fetch RFCnumber value and assign it to a variable.
Ex: From below code , I have to fetch 1200021992
for RFCNumber Property name and assign it to a variable.
<?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>
I need some help in this. I'm trying following code but it is not working:
$rfc = ($response -split "<d:RfcNumber>|</d:RfcNumber>")[X]
echo $rfc
Solution
1st solution: With your shown samples please try following awk
code.
awk -F'[><]'
'/<Property Name="RfcNumber" Type="System\.String">/{
print $3
}
' Input_file
OR if you have only 1 RefNumber
and you want to print only 1 value then better exit
from command to save sometime in that case you can try following awk
code a bit tweaked form of above:
awk -F'[><]'
'/<Property Name="RfcNumber" Type="System\.String">/{
print $3
exit
}
' Input_file
Explanation: Simple explanation would be, setting -F
(field separator) as >
and <
and in main block of program checking if line contains <Property Name="RfcNumber" Type="System.String"
then print 3rd field which is required output by OP.
2nd solution: Using sed
you can try following solution once.
sed -n '/<Property Name="RfcNumber" Type="System\.String"/{s/.*">//;s/<\/.*//p}' Input_file
OR in case you have only 1 match of the value in whole file then better to quit/exit from program rather than reading whole Input_file in that case try following:
sed -n '/<Property Name="RfcNumber" Type="System\.String"/{s/.*">//;s/<\/.*//p;q}' Input_file
Answered By - RavinderSingh13 Answer Checked By - Gilberto Lyons (WPSolving Admin)