Sed: Replace String in a File

This post will guide you how to search and replace a string with a new text string in Linux. How do I use sed tool to find and replace string with another string.

Replace String using Sed Tool


If you want to find the text string called “osetc” in fio.txt and replace to “osetcen”, how do I use sed tool to achieve the reuslt. You can use the following sed command with -i option to replace old_test with new_text:

# sed -i 's/osetc/osetcen/g' fio.txt

Outputs:

root@devops-osetc:/home/devops/working# cat fio.txt
osetc.com
this is osetc

root@devops-osetc:/home/devops/working# sed -i 's/osetc/osetcen/g' fio.txt
root@devops-osetc:/home/devops/working# cat fio.txt
osetcen.com
this is osetcen

If you do not update the original file using sed command, you should remove -i option, like below:

# sed  's/osetc/osetcen/g' fio.txt

Outputs:

root@devops-osetc:/home/devops/working# sed 's/osetc/osetcen/g' fio.txt
osetcen.com
this is osetcen

root@devops-osetc:/home/devops/working# cat fio.txt
osetc.com
this is osetc

 

You might also like:

Sidebar



back to top