I was looking at this solution: Using sed to edit lines in a file with a variable and tried to apply it to my situation, but I have only recently discovered sed and haven't got the hang of it yet.If I have a file located /tmp/mary.txt and it looks like this:Mary had a little lambwith fleece as white assnow. Everywhere thatMary went, the lamb wassure to go.I want to replace a line of text in this file, specifically:Mary had a little lamb -> Mary had 2 little lambsI used this:sed -i 's/*.had a little lamb.*/Mary had $(nproc) little lambs/' /etc/mary.txtI am using -i for inplace and s/ for substitute and it throws error:sed: 1: mary.txt: extra characters at the end of g commandThe other thing I am trying to do, is replace the word lamb on the first line with the result of echo $PATHsed -i 's/*.had a little lamb.*/ Mary had a little '$(echo $PATH)'/' /etc/mary.txtThis throws the same error as above. I know these are two different scenarios, but can it be shown to me what I did wrong, so I can use this in future.
Use sed to replace line of text with new line including variable
text processing;sed
Use double quotes to have the var evaluated properly:$ var=43$ sed s/.*had a little lamb.*/Mary had $var little lambs/ fileMary had 43 little lambswith fleece as white assnow. Everywhere thatMary went, the lamb wassure to go.See Jacob Minshall's answer regarding the .* instead of *.. I had written the sed command from scratch, so I did not notice the error in the original code.