Issue
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?
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:
Solution
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.
Answered By - arco444 Answer Checked By - Marie Seifert (WPSolving Admin)