12

I am trying replace value in a config file with sed in cshell.

However it gives me the error:

sed: 1: "/usr/local/etc/raddb/mo ...": extra characters at the end of l command

I am trying the following command:

sed -i "s/private_key_password = .*/private_key_password = test/" /usr/local/etc/raddb/mods-available/eap

I have looked at examples of sed to do this but they all look similar with what I am doing, what is going wrong here?

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146

1 Answers1

15

FreeBSD sed requires an argument after -i to rename the original file to. For example sed -i .orig 's/../../' file will rename he original file to file.orig, and save the modified file to file.

This is different from GNU sed, which doesn't require an argument for the -i flag. See sed(1) for the full documentation. This is one of those useful extensions to the POSIX spec which is unfortunately implemented inconsistently.

Right now, the "s/private_key_password = .*/private_key_password = test/" parts gets interpreted as an argument to -i, and /usr/local/etc/raddb/mods-available/eap gets interpreted as the command. Hence the error.

So you want to use:

sed -i .orig "s/private_key_password = .*/private_key_password = test/" /usr/local/etc/raddb/mods-available/eap

You can then check if the changes are okay with diff and remove /usr/local/etc/raddb/mods-available/eap.orig if they are.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • 3
    Passing an empty string to the `-i` flag will edit in-place i.e. `-i ''`. See https://stackoverflow.com/a/49934903/6548780 – muthuh Sep 11 '22 at 15:35