Pages

Tuesday, March 9, 2021

Sed Command in MAC

The sed command is used to manipulate and edit text in Unix-based operating systems like macOS.

Method to use Sed in Mac

sudo sed -i "/kube/d" /etc/hosts

give error sed: 1: "/etc/hosts": extra characters at the end of h command


In the first command you provided, sudo sed -i "/kube/d" /etc/hosts, the -i option tells sed to edit the file in place, and /kube/d is the command that tells sed to delete any lines containing the string "kube" in the /etc/hosts file. However, this command does not work on macOS as it requires the -i option to have a backup file extension specified. The error message "sed: 1: "/etc/hosts": extra characters at the end of h command" is indicating that the command is not properly formatted for macOS.

To fix this, you can use the following command: sudo sed -i '' '/kube/d' /etc/hosts. The empty quotes after -i specify that no backup file should be created, and the '' is necessary for macOS to recognize the command properly. The /kube/d command remains the same and tells sed to delete any lines containing the string "kube" in the /etc/hosts file.

Following Syntax Worked. 
sudo sed -i '' '/kube/d' /etc/hosts


No comments:

Post a Comment