How to Find and Replace Text using sed Command in Linux

In this post i will show to to find and replace text using sed command. We take /etc/host file as an example. We will replace any text “linodelinux.com” to “linodelinux.local” in /etc/hosts

# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.8.111   www.linodelinux.com
192.168.8.111   website1.linodelinux.com
192.168.8.111   website2.linodelinux.com

Just issue this command :

# sed -i 's/originaltext/newtext/g' file.txt

Example :

# sed -i 's/linodelinux.com/linodelinux.local/g' /etc/hosts

New /etc/host file as below :

# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.8.111   www.linodelinux.local
192.168.8.111   website1.linodelinux.local
192.168.8.111   website2.linodelinux.local

Leave a Comment