0

I'm trying to insert a new line into a file just before the last line. The new line includes tab and a variable.

Using the following. This inserts the line at the correct place, includes the tab but doesn't expand the variable.

sed '$i \\tnewline = $newvalue' file

If I wrap it in double quotes instead of single quotes I get errors. sed: -e expression #1, char 13: unterminated address regex

How do I do this ?

Thanks

Tom
  • 1,436
  • 24
  • 50
  • Hi, I've tried that and it generates the error I've posted. – Tom Sep 24 '21 at 14:45
  • please update the question with a sample input file, the output from `typeset -p newvalue` and the (correct) expected output – markp-fuso Sep 24 '21 at 14:47
  • It is not about difference in single and double quotes as OP has already tried double quotes. As per OP: `If I wrap it in double quotes instead of single quotes I get errors. sed: -e expression #1, char 13: unterminated address regex` – anubhava Sep 25 '21 at 08:40

1 Answers1

3

You may use this sed:

sed "\$i \\\\tnewline = $newvalue" file

Difference is escaping first $ when we use double quotes and use of \\\\t instead of \\t due to expansion of double quotes in shell.

anubhava
  • 761,203
  • 64
  • 569
  • 643