My input file is below
INPUT FILE
<ReconSummary>
<entryName>Total Deep</entryName>
<Code>777</Code>
<License>L</License>
<Tran>H20</Tran>
<job>1234</job>
</ReconSummary>
<ReconSummary>
<entryName>Total Saurav</entryName>
<Code>666</Code>
<License>L</License>
<Tran>H20</Tran>
<job>1234</job>
</ReconSummary>
I have code which looks like
LineNum=`grep -n "Deep" deep.xml | cut -d: -f 1 | awk -F '[\t]' '{$NF = $NF -1;}1'`
sed -i "/$LineNum/s/^/<!--/" deep.xml
What i need is:- i want to go to the line number which i am getting with LineNum variable and i want to add <!-- before an xml tag. So that my sample output looks like below
<!--<ReconSummary>
<entryName>Total Deep</entryName>
<Code>777</Code>
<License>L</License>
<Tran>H20</Tran>
<job>1234</job>
</ReconSummary>
<ReconSummary>
<entryName>Total Saurav</entryName>
<Code>666</Code>
<License>L</License>
<Tran>H20</Tran>
<job>1234</job>
</ReconSummary>
I am not able to do that. Can anyone tell what mistake i am doing in the code?
-
It is using XML parsing tool like xmlstarlet tool. But i want my script with shell in unixDEEP MUKHERJEE– DEEP MUKHERJEE2021年03月10日 15:51:41 +00:00Commented Mar 10, 2021 at 15:51
-
@DEEPMUKHERJEE Since this question is essentially asking for the same thing as your previous one, it will likely be closed as a duplicate of it. If you are not satisfied with the answer(s) you already have, please explain why in comments to those answers and, possibly, edit your original question to make your requirements more clear.fra-san– fra-san2021年03月10日 16:03:36 +00:00Commented Mar 10, 2021 at 16:03
1 Answer 1
You're inherently taking the wrong approach to handling XML files: use an XML parsing tool.
Nevertheless, for this specific problem: reverse the lines of the file; add the required prefix to the line after "Deep"; and re-reverse the output
tac file.xml | sed '/Deep/ {n; s/^/<!--/}' | tac
To save the result back into the original file, do one of:
sudo apt install moreutils
tac file.xml | ... | sponge file.xml
or
tmp=$(mktemp)
tac file.xml | ... > "$tmp" && mv "$tmp" file.xml
and as you are trying to comment the "Deep" paragraphs, use perl
perl -00 -lpe '$_ = "<!-- $_ -->" if /Deep/' file.xml