Command-line Basics: Finding and Replacing Text in Files with sed


Finding and replacing text inside of files is a pretty common task when you’re refactoring code. GUI editors, especially ones that support “projects” make this task fairly trivial. If your trusty editor doesn’t happen to support this, or if you find yourself needing to do a find and replace out on a remote server, you can do so quite easily with the sed command.

sed is short for “stream editor” and came on the scene back in 1974 and was influenced by the ed command.

You read that correctly, the sed command has been super charging the Unix command-line for nearly 50 years.

Getting started

Depending on your Unix-like system of choice, there’s a solid chance you already have sed available. If not, you should be able to install it easily with your favorite package manager.

Some of the following commands will explicitly be for the GNU implementation of sed which differs from the BSD implementation, like the version that ships with macOS.

If you’re on macOS, it’s recommended that you install the GNU version. The package is named gnu-sed if you’re using Homebrew. For the examples below, simply swap sed for gsed (or simply alias it) and you’ll be good to go.

You’re welcome to follow along at home using your own files, but just in case, let’s go ahead and create a test file to work with:

$ mkdir /tmp/gator-sed

$ cd /tmp/gator-sed

$ echo 'The quick brown fox jumped...' >> original.txt

$ cat original.txt

The quick born fox jumped...

Note that most of the commands we’ll be discussing will be non-destructive in nature. That said, we will also be covering how to edit files in place, which will overwrite the existing file.

It’s always good to make backups of your files before doing any extreme munging. sed does have a way to automatically make backups as well, which we’ll also touch on.

Finding and replacing text with a preview

Now I’m not saying I dislike foxes, but they definitely aren’t alligators. To give sed a whirl, let’s go ahead and replace “fox” with “alligator”.

By default, when you feed sed a file, it will output the changes to the standard output. This could be piped to a new file (which we’ll discuss later on) but it’s also a great opportunity to preview your changes to ensure they are accurate:

$ sed 's/fox/alligator/g' original.txt

The quick brown alligator jumped...

Case in point about previewing, let’s go ahead and change the color too:

$ sed 's/brown fox/green alligator/g' original.txt

The quick green alligator jumped...

Finding and replacing text to a new file

Now that we know how to find and replace text, to save it to a new file, we simply pipe the output to a new file:

$ sed 's/brown fox/green alligator/g' original.txt > new.txt

$ cat new.txt

The quick green alligator jumped...

Finding and replacing text in place

The aforementioned example provides a bit of safety as it doesn’t overwrite the original file, but that sort of safety isn’t always desired.

When you’re ready to live dangerously, we can edit the file in place by passing in the -i or --in-place argument:

$ sed 's/quick/fancy/g' new.txt -i

$ cat new.txt

The fancy green alligator jumped...

The -i argument also accepts an optional “suffix” value that is used to create backup files for you automatically:

$ sed 's/jumped/swam/g' new.txt -i.bak

$ cat new.txt

The fancy green alligator swam...

$ cat new.txt.bak

The fancy green alligator jumped...

Case-insensitive find and replace

While not immediately apparent, all of the replacements we’ve done thus far have been case sensitive.

If we were to try to replace the in our file, we wouldn’t be able to:

$ sed 's/the/A/g' new.txt

The fancy green alligator swam...

Fortunately, the GNU implementation of sed supports including an additional modifier flag in our find and replace string:

% sed 's/the/A/gi' new.txt

A fancy green alligator swam...

Unfortunately, BSD-based systems, including macOS and FreeBSD, use a different implementation of sed which doesn’t support the i modifier flag.

Multiple find and replace

Let’s say we wanted to replace the The as well as alligator to capitalize it. We could approach it by typing out everything between The and alligator as part of our find and replace text.

I’m lazy and like to save keystrokes, so we’re not going to do that.

Instead, we can chain together multiple find and replace strings:

% sed -e 's/The/A/g' -e 's/alligator/Alligator' new.txt

A fancy green Alligator swam...


Despite it’s age, the sed command packs quite the punch.

To take things a step further, you could combine the sed command with the power of wild cards * and other file globs to quickly and efficiently make text changes across multiple files and directories.

  Tweet It

🕵 Search Results

🔎 Searching...