On the Linux platform, I need to uncomment this occurrence within an xml file.
I can use the "sed" command to delete the comment on the first occurrence, but then I need to delete the comment also at the end of the tag: </Application-->
I cannot use the "sed" command globally, otherwise, I'll change also others lines commented on in the file.
There is a Linux command to uncomment the tag as per the example below?
<!-- Application id="MYPROGRAM_BATCH" appl="it.test.apps.batch.MyProgramBatch" logname="BATCH">
<Config>
<Active val="xxxxxxxxx"/>
<Interval val="xxxxx"/>
<TemplatePrefix val="xxxxxx" />
<FromAddress val="yyyy" />
<DefaultLang val="yyyyy" />
</Config>
</Application-->
Below is the sed command used to disable the comment on the first occurrence, but I don’t know how to uncomment also on the ninth line.
sed -i "s/<\!-- Application id=\"MYPROGRAM_BATCH\"/<Application id=\"MYPROGRAM_BATCH\"/g" myFileName.xml
How can I delete the comments also in the </Application-->
tag after 9 lines from the first occurrence?
CodePudding user response:
When you are sure about the number of lines, try this (add the -i
option when it works)
sed -rz 's%<\!-- Application id="MYPROGRAM_BATCH"(([^\n]*\n){8}[^<]*</Application)-->%<Application id="MYPROGRAM_BATCH"\1>%g' myFileName.xml
Explanation:
sed -rz
: Treat newlines as normal characters
s%...%...%
: The /
is used in the matched string, use another delimiter
(...)-->
: Remember everything that matches the inner expressions until -->
([^\n]*\n){8}
: Eight times a line including the newline at the end.
CodePudding user response:
This works:
sed -i '/<!-- \(Application id="MYPROGRAM_BATCH"\)/{s//<\1/;n;n;n;n;n;n;n;n;s/-->/>/}' myFileName.xml
It executes a substitution on the opening tag, skips 8 lines and performs the substitution on the closing tag.
This solution is fragile because in case the amount of lines between the opening and closing tag is not exactly 8, the code will be broken.
Anyway, your way to comment lines out by breaking the structure of the XML language is not a good idea: it prevents you from using tools that are more adapted to process XLM files than sed
.
If you had uncommented your lines of code as follows:
<!--<Application id="MYPROGRAM_BATCH" appl="it.test.apps.batch.MyProgramBatch" logname="BATCH">
<Config>
<Active val="xxxxxxxxx"/>
<Interval val="xxxxx"/>
<TemplatePrefix val="xxxxxx" />
<FromAddress val="yyyy" />
<DefaultLang val="yyyyy" />
</Config>
</Application>-->
you could have used XSLT transformations instead of sed
which is not adapted in most cases to process XML files.