0

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
  • 1
    Are you confusing the -i (--in-place) command line option with the i insert command by any chance? Nov 7, 2017 at 18:28

2 Answers 2

4

Why not just a simple echo?

$ echo 'HAVE_OPENBLAS = \usr' >> ./settings.mk
2
  • 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. Nov 7, 2017 at 18:18
  • How about echo "\\u"? For a simple use-case as this, I reckon weak-quoting is straightforward enough.
    – DopeGhoti
    Nov 7, 2017 at 18:44
2

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
    
2
  • 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, 2017 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. Nov 7, 2017 at 19:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .