I want to insert a text from variable at the certain line of a text file.
$cat foo
hello
hello
$var=`cat foo`
$echo "$var"
hello
$cat bar
some
text
I want to insert $var into second line, but sed does not read the content of the variable:
sed -i -e '2i$var\' bar
hello
$var
world
I guess because of two identical words in foo, I'm getting this:
sed: -e expression #1, char 11: extra characters after command
r
command, with its input from/dev/stdin
e.g.sed '1r /dev/stdin' bar <<< "$var"
. That way, there's no danger of the contents of$var
being treated as part of an expression. Of course in your example (where$var
consists of the contents of filefoo
), you can skip the variable altogether and just dosed '1r foo' bar
– steeldriver Jun 19 '18 at 11:32