I want to add an attribute to every tag in my xml, which is incrementing using either awk, sed, perl or plain shell cmd
For Eg:
<tag1 key="123">
<tag2 abc="xf d"/>
<tag3 def="d2 32">
</tag3>
</tag1>
I am expecting the following output
<tag1 key="123" order="1">
<tag2 abc="xf d" order="2"/>
<tag3 def="d2 32" order="3">
</tag3>
</tag1>
If possible I am not looking on any dependencies(Twig,LibXML), pure string manipulation.
Normally you should use a proper parser to process xml. But in
awk:I look for a opening tag (without the
>or/>part) on every the line. If found, put the stringorder="i"after it, while incrementingi. The single1on the last line just always executesawk‘s default action:{ print $0 }.I updated the regular expression to work on your revised input. It fails as soon as you have multiple opening tags on a single line, etc.