0

I tried to append a value to the ssh config file but it seems that it doesn't work if specify two different values, it reads the first not the second.

It seems sensible, instead of trying to re-write the entire file, to just modify the value.

Currently the value is:

Port 22

I want to go in and change the value to:

Port 222

The best I can come up with is this:

sed -c -i "s/\(Port *= *\).*/\1$REPLACEMENT_VALUE/" /etc/ssh/sshd_config

However I know this isn't right because it's working in a way where it is expecting to have an equals sign between the value and the variable.

I need the script to do something like this:

  • Look for the variable (i.e Port)
  • Switch this line with my own line

1 Answer 1

3

I think you mean this,

sed 's/.*\bPort\b.*/REPLACEMENT_VALUE/' file

Example:

$ REPLACEMENT_VALUE="Port 222"
$ echo -e "foo\nPort 22" | sed "s/.*\bPort\b.*/$REPLACEMENT_VALUE/"
foo
Port 222
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Do I need the i flag to change in place? Also the replacement value would read "Port 222" not "222", right?
add -i parameter to do an in-place edit. sed -i 's/.*\bPort\b.*/REPLACEMENT_VALUE/' file
if the replacement value is present inside a variable then you need to enclose the sed code within double quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.