0

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>


<ReconSummary>
<entryName>Total Ankur</entryName>
<Code>555</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>


<ReconSummary>
<entryName>Total Ankur</entryName>
<Code>555</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?

1

1 Answer 1

0

The first set of / in your sed line means "search for a line containing $LineNum"

What you really want is

sed -i "${LineNum}s/^/<!--/" deep.xml

Now that says "On line ${LineNum} do this s command..."

A very simple example where I replace the "e" on line 2 with the word "HELLO"

$ cat x
abcdef
abcdef
abcdef

$ sed '2s/e/HELLO/' x
abcdef
abcdHELLOf
abcdef
1
  • When i am using this "sed -i "${LineNum}s/^/<!--/" deep.xml" command i am getting sed: -e expression #1, char 7: unknown command: ` ' Commented Mar 10, 2021 at 13:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.