I do have a file that contains lines similar to the following:
<Item Name="INV_LIST" Justification="End" LowestAllowedValue="" DistanceBetweenRecords="0" Width="45" MaximumLength="12" SynchronizedItemName="INVOICE_AMT" PromptDisplayStyle="First Record"/>
<Item Name="INVOICE_AMT_LIST" Justification="End" LowestAllowedValue="" DistanceBetweenRecords="0" Width="48" MaximumLength="22" SynchronizedItemName="" PromptDisplayStyle="First Record"/>
<Item Name="INV_LIST2" Justification="End" LowestAllowedValue="" DistanceBetweenRecords="0" Width="233" MaximumLength="12" SynchronizedItemName="INVOICE_AMT2" PromptDisplayStyle="First Record"/>
I want to run a Linux command like sed or awk, to remove the attribute MaximumLength and its value (it does not matter what it contains between the quotes) whenever there is a line that contains a SynchronizedItemName with a value. If the line contains SynchronizedItemName="", the line will remain untouched.
I want to end with the following:
<Item Name="INV_LIST" Justification="End" LowestAllowedValue="" DistanceBetweenRecords="0" Width="45" SynchronizedItemName="INVOICE_AMT" PromptDisplayStyle="First Record"/>
<Item Name="INVOICE_AMT_LIST" Justification="End" LowestAllowedValue="" DistanceBetweenRecords="0" Width="48" MaximumLength="22" SynchronizedItemName="" PromptDisplayStyle="First Record"/>
<Item Name="INV_LIST2" Justification="End" LowestAllowedValue="" DistanceBetweenRecords="0" Width="233" SynchronizedItemName="INVOICE_AMT2" PromptDisplayStyle="First Record"/>
CodePudding user response:
You can try with this awk script:
{
where = match($0, "SynchronizedItemName=\"\"")
if (where != 0) print
else{
gsub(/MaximumLength=\"[0-9]*\"/, ""); print
}
}
Given your input, I get output as:
<Item Name="INV_LIST" Justification="End" LowestAllowedValue="" DistanceBetweenRecords="0" Width="45" SynchronizedItemName="INVOICE_AMT" PromptDisplayStyle="First Record"/>
<Item Name="INVOICE_AMT_LIST" Justification="End" LowestAllowedValue="" DistanceBetweenRecords="0" Width="48" MaximumLength="22" SynchronizedItemName="" PromptDisplayStyle="First Record"/>
<Item Name="INV_LIST2" Justification="End" LowestAllowedValue="" DistanceBetweenRecords="0" Width="233" SynchronizedItemName="INVOICE_AMT2" PromptDisplayStyle="First Record"/>
CodePudding user response:
If you have xmlstarlet available, you could use the ed
command...
NOTE: The "-L" global option modifies the file in-place. Remove this if you do not want to modify the original input file.
xmlstarlet ed -L -d '//Item[normalize-space(@SynchronizedItemName)]/@MaximumLength' input.xml