0

I have test.txt file and I want to add my variable to specific line after the word. But my sed command not work how can I do this?

test.txt file is:

Hello:
Green:
Yellow:

My variable is

echo $variable

http://111.111.111.11

My bash command is:

sed '/Green:/r/dev/stdin' test.txt <<<"$variable"

My output after run the bash command:

Hello:
Green:
http://111.111.111.11
Yellow:

My desire output:

Hello:
Green:http://111.111.111.11
Yellow:
oguz ismail
  • 1
  • 16
  • 47
  • 69
hammer89
  • 343
  • 1
  • 5
  • 13

1 Answers1

1

You can place your variable directly into the substitution:

sed "s~\(Green:\)~\1$variable~" test.txt

This will match the string Green: and capture it into group \1, you can then append $variable to the captured group in the substitution. Note I have used ~ as a separator as your substitution string contains multiple slashes / which cause problems if you also use / as the separator.

arco444
  • 22,002
  • 12
  • 63
  • 67