33

I'm reading a lot of documentation on sed, and am still stumped on my particular use case.

I want to replace this line in a conf file with my own line:

Replace this line:

#maxmemory <bytes>
with:
maxmemory 26gb

This is what I tried:

sed s/maxmemory.*bytes.*/maxmemory 26gb/ /etc/redis/redis.conf

I get the error:

sed: -e expression #1, char 30: unterminated `s' command

Which stumps me because I don't know what that means. So my question is:

How can I accomplish what I want? What does that error mean? (so I can learn from it)

JdeBP
  • 26,613
  • 1
  • 72
  • 103
Henley
  • 343
  • 1
  • 3
  • 8

3 Answers3

38

You have forgot -i. Modification should be made in place :

$ sed -i 's/maxmemory.*/maxmemory 26gb/' /some/file/some/where.txt

Phantom
  • 381
  • 3
  • 2
  • And if you have slashes in your strings, you must use another delimiter character instead of `/`, such as `@` or `|`. See here: https://stackoverflow.com/a/9366940/4561887 and here: https://unix.stackexchange.com/a/259087/114401. – Gabriel Staples Mar 18 '20 at 05:23
  • Concise and precisely what one will typically need. Thanks a lot, @Phantom. – f4f Nov 03 '20 at 06:11
12

Indeed

The error means that in the absence of quotes, your shell uses spaces to separate arguments. The space between maxmemory and 26gb is thus considered as terminating the first argument, which thus lacks a terminal / when sed comes to parse that argument as one of its commands.

Putting your regex between single quotes, so that your shell doesn't split it into multiple arguments and hands it to sed as one single argument, solves the problem:

$ sed s/maxmemory.*/maxmemory 26gb/ /some/file/some/where.txt
sed: -e expression n°1, caractère 23: commande `s' inachevée

while

$ sed 's/maxmemory.*/maxmemory 26gb/' /some/file/some/where.txt

works.

Hope that helps.

JdeBP
  • 26,613
  • 1
  • 72
  • 103
Sxilderik
  • 311
  • 1
  • 6
3

Your use case will be solved by this command.

sed -i -e 's/#maxmemory.*/maxmemory 26gb/g' /etc/redis/redis.conf
davidbaumann
  • 2,189
  • 1
  • 14
  • 32