I am a new Linux user. I wanted to find the text called “foo” and replaced to “bar” in the file named “hosts.txt.” How do I use the sed command to find and replace text/string on Linux or UNIX-like system?

The sed stands for stream editor. It reads the given file, modifying the input as specified by a list of sed commands. By default, the input is written to the screen, but you can force to update file.

Find and replace text within a file using sed command

The procedure to change the text in files under Linux/Unix using sed:

  1. Use Stream EDitor (sed) as follows:
  2. sed -i 's/old-text/new-text/g' input.txt
  3. The s is the substitute command of sed for find and replace
  4. It tells sed to find all occurrences of ‘old-text’ and replace with ‘new-text’ in a file named input.txt
  5. Verify that file has been updated:
  6. more input.txt

Let us see syntax and usage in details.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements sed utility on Linux, macOS or Unix-like OS
Est. reading time 4 minutes

Syntax: sed find and replace text

The syntax is:
sed 's/word1/word2/g' input.file
## *bsd/macos sed syntax#
sed 's/word1/word2/g' input.file > output.file
sed -i 's/word1/word2/g' input.file
sed -i -e 's/word1/word2/g' -e 's/xx/yy/g' input.file
## use + separator instead of / ##
sed -i 's+regex+new-text+g' file.txt

The above replace all occurrences of characters in word1 in the pattern space with the corresponding characters from word2.

Examples that use sed to find and replace

Let us create a text file called hello.txt as follows:
$ cat hello.txt
The is a test file created by nixCrft for demo purpose.
foo is good.
Foo is nice.
I love FOO.

I am going to use s/ for substitute the found expression foo with bar as follows:
sed 's/foo/bar/g' hello.txt
Sample outputs:

The is a test file created by nixCrft for demo purpose.
bar is good.
Foo is nice.
I love FOO.

To update file pass the -i option:
sed -i 's/foo/bar/g' hello.txt
cat hello.txt

The g/ means global replace i.e. find all occurrences of foo and replace with bar using sed. If you removed the /g only first occurrence is changed:
sed -i 's/foo/bar/' hello.txt
The / act as delimiter characters. To match all cases of foo (foo, FOO, Foo, FoO) add I (capitalized I) option as follows:
sed -i 's/foo/bar/gI' hello.txt
cat hello.txt

Sample outputs:

The is a test file created by nixCrft for demo purpose.
bar is good.
bar is nice.
I love bar.

Please note that the BSD implementation of sed (FreeBSD/MacOS and co) does NOT support case-insensitive matching. You need to install gnu sed. Run the following command on Apple Mac OS:
$ brew install gnu-sed
### now use gsed command as follows ##
$ gsed -i 's/foo/bar/gI' hello.txt
$ cat hello.txt

sed command problems

Consider the following text file:
$ cat input.txt
http:// is outdate.
Consider using https:// for all your needs.

Find word ‘http://’ and replace with ‘https://www.cyberciti.biz’:
sed 's/http:///https://www.cyberciti.biz/g' input.txt
You will get an error that read as follows:

sed: 1: "s/http:///https://www.c ...": bad flag in substitute command: '/'

Our syntax is correct but the / delimiter character is also part of word1 and word2 in above example. Sed command allows you to change the delimiter / to something else. So I am going to use +:
sed 's+http://+https://www.cyberciti.biz+g' input.txt
Sample outputs:

https://www.cyberciti.biz is outdate.
Consider using https:// for all your needs.

How to use sed to match word and perform find and replace

In this example only find word ‘love’ and replace it with ‘sick’ if line content a specific string such as FOO:
sed -i -e '/FOO/s/love/sick/' input.txt
Use cat command to verify new changes:
cat input.txt

Recap and conclusion – Using sed to find and replace text in given files

The general syntax is as follows:
## find word1 and replace with word2 using sed ##
sed -i 's/word1/word2/g' input
## you can change the delimiter to keep syntax simple ##
sed -i 's+word1+word2+g' input
sed -i 's_word1_word2_g' input
## you can add I option to GNU sed to case insensitive search ##
sed -i 's/word1/word2/gI' input
sed -i 's_word1_word2_gI' input

See BSD(used on macOS too) sed or GNU sed man page by typing the following command:
man sed

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • GL May 29, 2017 @ 10:21

    Regarding the problem with slashes, you could also escape them like this:
    sed 's/http:\/\//https:\/\/www.cyberciti.biz/g' input.txt

  • Vasanth R Aug 30, 2017 @ 20:38

    How to do the same for multiple files

    sed ‘s+http://+https://www.cyberciti.biz+g’ *.php

    I want to change http to https in all the files. I have around 1000+ files.

    • 🐧 Vivek Gite Aug 31, 2017 @ 7:39

      What is the problem with this command? It should work. Replace http://www.cyberciti.biz with your actual domain name. As always keep backup of all files ;)
      sed 's+http://+https://www.cyberciti.biz+g' *.php

  • DEB Aug 21, 2020 @ 22:05

    I have a csv file, and some rows have values like 23 BT, 6 89 HT 67. I want to delete them which has space in between. Please help

  • Zim Jones Feb 22, 2021 @ 17:21

    I wanted to know how to use sed to find and replace string in files on macOS. I had couple of xml file and needed update. This page was very helpful.

  • Tank Arstogi Mar 26, 2021 @ 3:19

    Very helpful. May God bless fraternity with prosperity.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum