How to use variables in 'sed' append command?

HELLO!!

I'm trying to pass a variable with in the 'sed' command (which would add some piece of code to file at a particular line). We can use

sed '{line-number}a\
alfjaljf\
aslfjsfsjafl\
adlfjaf\' file.txt

If file.txt is

Now, I would like to add the parameter 'lmn' after 'abc' property. I tried this:

sed '4a\
\t<Property>\
\t\t<name>lmn</name>\
\t\t<value>90<\/value>\
\t<\/Property>' file.txt > file1.txt

But I want to grep the line number of 'abc' and then based on the line number i would like to perform the append using sed command.
here is my code:

#! /bin/bash
a=`grep -n "<name>abc</name>" file.txt` | awk '{print $1}'`
b=`echo ${a%?}`
c=`expr b+2`
sed '$ca\
\t<Property>\
\t\t<name>lmn</name>\
\t\t<value>90<\/value>\
\t<\/Property>' file.txt > file1.txt

But this is not working. Can anyone help me please??? :confused::confused:

Put $c outside of single quotes:

#! /bin/bash
a=`grep -n "<name>abc</name>" file.txt` | awk '{print $1}'`
b=`echo ${a%?}`
c=`expr b+2`
sed $c'a\
\t<Property>\
\t\t<name>lmn</name>\
\t\t<value>90<\/value>\
\t<\/Property>' file.txt > file1.txt
1 Like