How to Use Sed to Find and Replace String

Friday, Aug 9, 2024 | 3 minutes read | Update at Friday, Aug 9, 2024

@ İsmail Baydan

Sed is one of the most popular text editor in Linux. Sed provides more than text editor with its flexibility to edit specified content in a pattern based way. One of the most popular usage for sed is finding and replacing string in a text file.Here are 10 examples of using the sed command in Ubuntu to perform find-and-replace operations:

1. Basic Find and Replace

The most basic and popular usage of the find and replace for sed is below. The s/ is used to search foo and replace with the bar . Slashes are used to separate these definitions. This will only find and replace first occurance of the foo

sed 's/foo/bar/' file.txt

Replaces the first occurrence of “foo” with “bar” in each line of file.txt.

2. Find and Replace with Global Flag

We can use the g or global flag to find and replace all occurences of the specified term.

sed 's/foo/bar/g' file.txt

Replaces all occurrences of “foo” with “bar” in each line of file.txt.

3. Replace Only in a Specific Line

We can also restrict the find and replace only with specified line. In the following example we only find and replace the 3rd line in the file.

sed '3s/foo/bar/' file.txt

Replaces “foo” with “bar” only in the 3rd line of file.txt.

4. Case-Insensitive Find and Replace

By default the find operation is case sensitive where lower case and upper case are treated as different. We can disable the case sensitiveity and make case-insensitive with the /I at the end of the statement.

sed 's/foo/bar/I' file.txt

Replaces “foo” with “bar” regardless of the case (e.g., “Foo” or “FOO”).

5. Replace and Save to a New File

By default the new content printed to the standard output which is generally the terminal. We can save the next text into a new file by using the > redirection.

sed 's/foo/bar/g' file.txt > newfile.txt

Replaces “foo” with “bar” globally and saves the output to newfile.txt.

6. Replace Only in Lines Matching a Pattern

We can specific a pattern to find and replace. In the following example the foo will be replaces with the bar for those lines matched patterns .

sed '/pattern/s/foo/bar/' file.txt

Replaces “foo” with “bar” only in lines that contain “pattern”.

7. Replace with a Backreference

sed 's/\(foo\)/\1bar/' file.txt

Appends “bar” to “foo” using a backreference.

8. Replace a Word Only if It’s a Whole Word

By default the search text will be search whole or part of the word. We can make the search as whole word with the \b option like below.

sed 's/\bfoo\b/bar/g' file.txt

Replaces “foo” with “bar” only if “foo” is a whole word.

9. Replace Delimiters with Another Delimiter

On of the most popular use case for the sed is replacing delimiters. We can replace one delimiter with another. In the following exmaple we replace / with the - .

sed 's|/|-|g' file.txt

Replaces all slashes (/) with dashes (-) globally.

10. In-Place Replacement

sed -i 's/foo/bar/g' file.txt

Performs an in-place replacement of “foo” with “bar” globally in file.txt.

These examples demonstrate different ways to use sed for find-and-replace tasks, covering common scenarios and options available in the command.

© 2024 Linux and Python Tutorials

🌱 Powered by Hugo with theme Dream.