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 lamb
with fleece as white as
snow. Everywhere that
Mary went, the lamb was
sure to go.
I want to replace a line of text in this file, specifically:
Mary had a little lamb -> Mary had 2 little lambs
I used this:
sed -i 's/*.had a little lamb.*/Mary had $(nproc) little lambs/' /etc/mary.txt
I am using -i for inplace and s/ for substitute and it throws error:
sed: 1: "mary.txt": extra characters at the end of g command
The other thing I am trying to do, is replace the word "lamb" on the first line with the result of echo $PATH
sed -i 's/*.had a little lamb.*/ Mary had a little '$(echo $PATH)'/' /etc/mary.txt
This 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.