Remote SSH command with sed not working

In the remote server, I can run this command:

sed -i '/address/c\address xx.xx.xx.xx' /myfile

to replace any line containing address with address xx.xx.xx.xx in /myfile

However, from my local machine, this command is not working

ssh root@ip sed -i '/address/c\address xx.xx.xx.xx' /myfile

It gives the following error:

sed: can't read xx.xx.xx.xx: No such file or directory

How to fix this problem?

2 Answers

You are preventing the expansion on the local shell, but you need to preserve everything as far as the command to run, sed in this case, on both shells.

Quote the sed command to run:

ssh root@ip "sed -i '/address/c\address xx.xx.xx.xx' /myfile"

In essence, any quoting method over the current would do too, but using double quotes like above would be simpler and cleaner.

0

To replace u should use sed in this way:

sed -i 's/^.*address/address yourfile

This regex should find "address" in your file and replace it for "address xx.xx.xx.xx".

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like