Insert multiple lines Containing ' and $variable using sed not working

Viewed 48

I am new to scripting and stuck at one place that may be really simple.. still would be grateful if anyone can help.

Below is my issue in simplest term:

Input file new.txt

Hello team

Output file expected: new_2.txt

  Select '/backup/path1_' from dual;
  Select '/backup/path2_' from dual;  
    Hello team 

Note : $var1=path1 and $var2=path2

Sed command used : 
Sed '1i\
Select '/backup/"$var1"_' from dual;\
Select '/backup/"$var2"_' from dual;\
' new.txt > new_2.txt

Output received:

new_2.txt 
Select /backup/path1_ from dual;
Select /backup/path2_from dual;

Hello team 

After various quotes combination also, either single quote ' won't be displayed in output or var value won't be inserted.

2 Answers

Would you please try the following:

var1=path1
var2=path2
sed "1i\\
Select '/backup/${var1}_' from dual;\\
Select '/backup/${var2}_' from dual;
" new.txt > new_2.txt

Result:

Select '/backup/path1_' from dual;
Select '/backup/path2_' from dual;
Hello team

You can also escape the quotes mark with a backslash:

sed '1i\
Select '\'/backup/"$var1"_\'' from dual;\
Select '\'/backup/"$var2"_\'' from dual;
' new.txt > new_2.txt
Related