Remote Ssh Command with Sed Not Working

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

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

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.