Find and Replace Text with Sed in Bash

Cameron Nokes
InstructorCameron Nokes
Share this video with your friends

Social Share Links

Send Tweet

sed, which stands for stream editor, is probably most commonly used to replace text patterns with a new one, which is exactly what we’ll learn in this lesson.

You'll learn how what flags you pass, the address argument as well as the commands that sed takes. The sed syntax can be summarized like this: sed [flags] '[address]command[options]' [input file]

Instructor: [0:00] Sed, which stands for stream editor, is probably most commonly used to find and replace strings. Let's look at how to do that.

[0:06] I have a file here, file.txt. Let's say I want to replace all occurrences of fox, which I have two, with the word dog. To do that, we invoke sed. Then from my sed scripts, I wrapped that in single quotes, so that any special symbols in that script don't get interpreted by bash itself. I just want them to pass verbatim to the sed command.

[0:30] I do s, which stands for substitute. Then I'll do fox, replace fox with dog. Right here, this is my regex. This is my replacement. Then this is the global flag. When we pass our file name there, we see that works.

[0:45] It outputs the modified text to standard out. Now, instead of fox, we have dog here if we had a replace right here as well. The actual file is unchanged. If we want to change it in-place, we use the i flag. Do send -i.

[1:03] Here's a caveat. If you're on Linux, this will work as is. Most Linux distributions use a newer version of sed where you can use this in-place flag without any arguments, and it'll just modify this file.

[1:16] On Mac OS however, the value to the in-place flag here is not optional. You have to provide an argument to it. I'm on Mac OS, so this is going to fail. To get around that, we just pass an empty string here. If I run it, you can see that works.

[1:34] The in-place flag can be handy though. The argument that you provide becomes the extension of a backup file that is created with the original source. Right here, if I do .bak...Let's do something else. Let's reverse it. Let's go from dog to fox.

[1:50] We do that. We list out our files. We have the file.txt right here, and then we have this new .bak file. If we output the bak file, we can see we have the original source here. Then if we output this, this is our modified one. You could also put a line number before the command to specify which lines you would like the command to affect.

[2:12] If I do sed and I pass 1 for my line number...I can optionally put a space right here. We do substitute. We do that globally, pass our file. You can see that that just affected the first line. Those are both foxes. Now they're dogs, but the fox right here is unchanged.

[2:30] Note that we can also do a range of line numbers. If I wanted that second line to be affected...The ranges are comma-separated. This is lines 1-2. We can also use a regex to dynamically match on what lines we want the command to affect.

[2:46] First, let's scan out our file again so we can see it. We do sed. Then let's put in our regex. I want a match on every line that contains a lowercase quick, which is just the first line. This is a case-sensitive match. We can see that worked. The two foxes in the first line got replaced, but the second line was unaffected.

[3:05] Note that regexes can be inverted. I can say, "Every line that doesn't match my regex." Now we get the inverse. We can see this fox is replaced with a dog, but these two are left unchanged.

[3:19] We can see that sed is a specialized programming language for manipulating text. If we take a step back, we can basically summarize the sed syntax like this. We have the sed commands. Optionally, we have flags we can pass to it, like the i flag. Then we have our sed script.

[3:35] We have address, which is optional. We could have no address, or we can have a line number, a range of line numbers, or a regex. We have commands, like substitute. Commands are always one letter, like s for substitute, i for insert, d for delete, etc. Some commands have options you can pass to them.

[3:54] After that, we optionally have an input file. The input file is optional because we can pipe to sed. When you understand at a higher level how sed's terse syntax works, it makes remembering how to use it much easier.