How to Use Sed to Find and Replace a String in a File

September 22, 2022

Introduction

The sed (stream editor) utility is a line-oriented text parsing and transformation tool. The sed command uses a simple programming language and regular expressions to process text streams and files. The most used feature of the sed command is string substitution.

This guide shows how to use sed to find and replace strings through examples.

How to Use Sed to Find and Replace a String in a File

Prerequisites

  • Access to the command line/terminal.
  • A text file (this guide provides an example.txt file).
  • Basic terminal commands (grab our free cheat sheet).

sed Find and Replace Syntax

The syntax to find and replace text using the sed command is:

sed -i 's/<search regex>/<replacement>/g' <input file>

The command consists of the following:

  • -i tells the sed command to write the results to a file instead of standard output.
  • s indicates the substitute command.
  • / is the most common delimiter character. The command also accepts other characters as delimiters, which is useful when the string contains forward slashes.
  • <search regex> is the string or regular expression search parameter.
  • <replacement> is the replacement text.
  • g is the global replacement flag, which replaces all occurrences of a string instead of just the first.
  • <input file> is the file where the search and replace happens.

The single quotes help avoid meta-character expansion in the shell.

The BDS version of sed (which includes macOS) does not support case-insensitive matching or file replacement. The command for file replacement looks like this:

sed 's/<search regex>/<replacement>/g' <input file> > <output file>

Alternatively, install the GNU version of sed on macOS with homebrew:

brew install gnu-sed

Run the GNU sed command as follows:

gsed -i 's/<search regex>/<replacement>/g' <input file>

Replace the sed command with gsed to follow the examples below.

sed Replace Examples

The examples from this guide use a sample file to replace strings.

1. Create a sample text file:

nano example.txt

2. Add the following contents:

foobarbazfoobarbaz
foo bar baz foo bar baz
Foo Bar Baz Foo Bar Baz
FOO BAR BAZ FOO BAR BAZ
/foo/bar/baz /foo/bar/baz
cat example.txt sed file terminal output

Use the file as input to test the examples below.

Note: For a full sed command tutorial for Linux, check out our sed command Linux guide.

Replace First Matched String

1. To replace the first found instance of the word bar with linux in every line of a file, run:

sed -i 's/bar/linux/' example.txt

2. The -i tag inserts the changes to the example.txt file. Check the file contents with the cat command:

cat example.txt
sed replace bar in file terminal output

The command replaces the first instance of bar with linux in every line, including substrings. The match is exact, ignoring capitalization variations.

Global Replace

To replace every string match in a file, add the g flag to the script. For example:

sed -i 's/bar/linux/g' example.txt
sed replace bar file global terminal output

The command globally replaces every instance of bar with linux in the file.

Match and Replace All Cases

To find and replace all instances of a word and ignore capitalization, use the I parameter:

sed -i 's/bar/linux/gI' example.txt
sed replace bar case insensitive terminal output

The command replaces all instances of the word bar in the text, ignoring capitalization.

Ignore Substrings

Add word boundaries (\b) to the sed command to ignore substrings when replacing strings in a file. For example:

sed -i 's/\bbar\b/linux/gI' example.txt

Alternatively, change the delimiter to make the command easier to read:

sed -i 's:\bbar\b:linux:gI' example.txt
sed replace bar file ignore substring terminal output

The command ignores substrings, matching only the whole word.

Find and Replace Strings With Slashes

Escape the forward slash character to find and replace a string with slashes. For example, to replace /foo/bar/baz with /home/kb, use the following syntax:

sed -i 's/\/foo\/bar\/baz/\/home\/kb/g' example.txt

Alternatively, change the delimiter to avoid escaping characters:

sed -i 's:/foo/bar/baz:/home/kb:g' example.txt
sed replace path string file terminal output

Use this syntax to replace paths and other strings with slashes.

Find and Replace with Regular Expressions

The search pattern for the sed command accepts regular expressions, similar to grep regex. For example, to match all capital letters and replace them with 5, use:

sed -i 's/[A-Z]/5/g' example.txt
sed file replace regex terminal output

The regex pattern helps find all capital letters and replaces them with the number in the file.

Reference Found String

Use the ampersand character (&) to reference the found string. For example, to add a forward slash (/) before every instance of foo in a file, use:

sed -i 's/foo/\/&/gI'example.txt
sed file replace string reference terminal output

Instead of retyping the search parameter, the & sign references the found string.

Create a Backup

To create a backup file before overwriting the existing one, add the .bak parameter to the -i tag.

sed -i.bak 's/foo/FOO/g' example.txt
sed replace backup file terminal output

The command creates a backup (example.txt.bak) before overwriting the original. Use this method to keep a copy in the original format and avoid overwriting.

Recursive Find and Replace

Use the find command to search for files and combine it with sed to replace strings in files recursively. For example:

find -name 'example*' -exec sed -i 's/[a-z]/5/gI' {} +
sed file replace recursive terminal output

The command finds all files starting with example and executes the sed command on the files. The executed command replaces all letters with 5, ignoring capitalization.

Conclusion

After going through the examples in this guide, you know how to use sed to replace strings in files. The sed command is a powerful text manipulation utility with many advanced features.

Next, learn how to use sed to delete a line from a text file or check out the awk or gawk command to learn about other text manipulation tools.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
AWK Command in Linux with Examples
October 28, 2021

This tutorial shows how to use the awk command in Linux to perform advanced...
Read more
How to Read Files Line by Line in Bash
March 17, 2022

This tutorial shows how to read a file line by line using the Bash shell. See five...
Read more
How to Rename Files in Linux
November 20, 2023

There are several ways to rename files in Linux, including using the GUI and terminal commands. This tutorial shows you how to rename...
Read more
How to Find Files in Linux With the Find Command
December 19, 2018

The find command is a useful command-line tool in Linux. This tutorial shows how to use the find...
Read more