How to print & and \n while replacing with sed/awk?

string="din&esh\nisgood"

File.txt:

the name is [Name]
sed "s#\[Name\]#${string}#g" File.txt

Output am getting:

the name is din[Name]esh
is good

Expected output:

the name is din&esh\nisgood

The input string is dynamic it will be keep on changing

am able to handle & by placing \& in the string..

Hello dineshaila,

Could you please try following and let me know if this helps you.

string="din&esh\nisgood"
awk -vString=$string '{sub(/\n/,"",String);sub(/&/,"\\&",String);sub(/\[Name\]/,String);print}' Input_file

Thanks,
R. Singh

1) excape the & with a backslash, and escape the \n 's backslash as well
2) use single quote to prevent expansion by the shell

string='din\&esh\\nisgood'
sed "s#\[Name\]#${string}#g" file
the name is din&esh\nisgood

I have tried escaping the & and \n
the output is
the name is din&esh
isgood

---------- Post updated at 11:14 AM ---------- Previous update was at 11:12 AM ----------

Hi Ravinder,
u r replacing the \n with blank.. but i need \n in the output

Also my input string will be dynamically changing
it may or may not have \n and &
it may have other regex
My output should display all the characters in string. it should be replaced with regex

Hello dineshaila,

Solutions always being provided on the sample Input_file shown and conditions provided, in your first post it was no where mentioned that there could be permutations and combinations. Kindly mention all the conditions of your requirement and with very near sample Input_file so that we could try to help you in spite of guessing each time.

Thanks,
R. Singh

string='din\&esh\\nisgood'
sed "s#[[]Name[]]#${string}#g" infile