Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

I was looking at this solution: Using sed to edit lines in a file with a variableUsing 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.

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.

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.

clean up
Source Link
Timo
  • 6.5k
  • 1
  • 28
  • 30

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.

Thank you.

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.

Thank you.

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.

fixed tags
Link
terdon
  • 252.3k
  • 69
  • 480
  • 718
typo
Link
Stéphane Chazelas
  • 585k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Danijel-James W
  • 1.8k
  • 5
  • 16
  • 20
Loading