I'm trying to append a line of text to a settings files but I keep getting error sed: -e expression #1, char 2: extra characters after command

Here is what I'm doing:

sed -i 'HAVE_OPENBLAS = \usr' ./settings.mk

Any help is appreciated!

  • 1
    Are you confusing the -i (--in-place) command line option with the i insert command by any chance? – steeldriver Nov 7 '17 at 18:28

Why not just a simple echo?

$ echo 'HAVE_OPENBLAS = \usr' >> ./settings.mk
  • The behaviour of echo when its arguments contain backslashes is unspecified by POSIX. In some echo implementations, that echo '\u' would be short for echo '\u0000' and output a NUL character. Use printf instead. – Stéphane Chazelas Nov 7 '17 at 18:18
  • How about echo "\\u"? For a simple use-case as this, I reckon weak-quoting is straightforward enough. – DopeGhoti Nov 7 '17 at 18:44

If you absolutely must do it with sed:

  • to add the line at the beginning of file:

    sed -i -e '1i\' -e 'HAVE_OPENBLAS = \\usr' ./settings.mk
  • to add the line at the end of file:

    sed -i -e '$a\' -e 'HAVE_OPENBLAS = \\usr' ./settings.mk
  • to add the line as line 5, and shift down subsequent lines:

    sed -i -e '5i\' -e 'HAVE_OPENBLAS = \\usr' ./settings.mk
  • to replace line 5 in the file with the new line:

    sed -i -e '5c\' -e 'HAVE_OPENBLAS = \\usr' ./settings.mk
  • I need to add it to the end of the file but whenever I do sed -i -e '$a\' -e 'HAVE_OPENBLAS = \\usr' ./settings.mk I get nothing writtern to my settings file – 4rb3l Nov 7 '17 at 18:24
  • Then your sed doesn't support option -i. Either you aren't using GNU sed, or you're using a really old version of it. – Satō Katsura Nov 7 '17 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.