How to Find and Replace Text in a File Using the the sed Command in Linux

Learn how to use the "sed" command to Find and Replace Text in a File in Linux operating system.

How do I use the sed command on a Linux or UNIX-like system to find and replace text or a string?

 

I am a new to Linux and UNIX Operating System. I wanted to find the text called “foo” and replaced to “bar” in the file named “option.txt.”

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.

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

Use Stream Editor (sed) as follows: sed -i 's/old-text/new-text/g' words.txt The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of old-text and replace with new-text in a file named words.txt

Verify that file has been updated: more words.txt Let us see syntax and usage in details.

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.filesed -i 's/word1/word2/g' input.filesed -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 howdy.txt as follows: $ cat howdy.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.

sed find and replace examples for unix and linux

To update file pass the -i option: sed -i 's/foo/bar/g' hello.txtcat 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.txtcat 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 problemsConsider the following text file:$ cat input.txt`

http:// is outdate. Consider using https:// for all your needs.

ALSO READ: All About the VI Editor and VI Commands in Linux/Unix

Find word ‘http://’ and replace with ‘https://www.braghq.com’: sed 's/http:///https://www.braghq.com/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' inputsed -i 's_word1_word2_g' input ## you can add I option to GNU sed to case insensitive search ## sed -i 's/word1/word2/gI' inputsed -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

Comments