Home > Enterprise >  BASH - SED insert new line before last, containing tab and variable
BASH - SED insert new line before last, containing tab and variable

Time:09-25

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

CodePudding user response:

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.

  • Related