Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie, we will take a look at a Unix program called sed. sed is short for Stream Editor. What that means is that sed modifies a stream of input according to a list of commands before passing it on to the output. It's very similar to the way that tr works, but with sed, we can modify the stream of input in many more ways. sed is a complex tool that offers several modes of working and mini features. In this movie, we are just going to focus on the basics and get to see the most common usage. It's most common usage is for doing substitution and so what we'll be doing is sed, space, and then providing an expression for substituting.
That will always begin with the letter s inside the quotes to indicate that it's going to be doing substitution. Then we have got some delimiters, the forward slash in my example, and then we've got patterns of searching and replacing, just like we had with tr. So we are going to search for whatever is in a and replace it with whatever is in b. Notice that the big difference between this and tr though is that tr actually had two different arguments. It had tr, then the first argument, and then the second argument. Here, they're all in one argument and they are inside those delimiters.
sed is really based on this one expression. So everything that we wanted to do, the sort of command that we are sending to sed, is going to be inside those quotes. Let's take a look at some examples. So, for example, let's take some input, echo upstream, and we are going to pipe that into sed. So then what we needed here is an expression inside quotes for substitution. I am going to go ahead and put my delimiters. Now, inside each of these, we are going to need to fill in what we are searching for. I am going to search for up and we want to replace it with down.
So, upstream becomes downstream, which is appropriate since we are doing stream modification. Upstream, it was upstream. Once it gets downstream, it's downstream. In between is sed doing the stream editing. Now, notice how this is different from what we were doing with tr. tr was translating each of those. What we are doing here is we are searching for one thing and we are replacing it with something else. It's much more like the find and replace that you might do in a word processing program. Now there is one important thing I need to show you about sed, which is that by default it doesn't search globally.
I will show you what I mean. So let's say we have upstream and upward and then let's just hit Return. Notice now it changed the first occurrence of up, not both of them, just the first one got changed. That's different from what we were doing with grep. When we were greping for something, it was finding all occurrences of it and that's because in grep, the g stands for global. It's globally searching. So, just like grep, we need to provide a g modifier here to say that we want to do this globally. So after this pattern of s and then a replace string, then we put a g to say that we would like to do that globally. Downstream and downward now does both of them.
Now, the delimiters that we are using here, these forward slashes, actually are modifiable. It doesn't actually matter which one we have. Let's do, for example, colons. What sed does is it says, all right, what's the first thing that comes after the s? All right, I see the s, I know we are going to do a substitution, what delimiters should I use? And whatever we have is that next character is what it will use as the delimiter. So, we can use pipes like that. It still works. I will just paste in an example to show you why you might want to do this.
The forward slashes are really the most traditional usage of it. But let's say, for example, that I had a phrase like MacOS/Unix: awesome and I wanted to change that. Then the search string that I'm looking for has a forward slash and a colon in it in it, so I don't wan to use either of those first two choices. Therefore I'm using the upright pipes. Now, I could still use the forward slashes. I will just have to escape each of those values. Every time it occurred, I would have to put a backslash before the forward slash to escape it. By switching to a different delimiter, I can just make it a little cleaner and easier to read.
So far, the stream that I've been using has been texted that I'm piping into sed, but sed also works with files. So, let's say we have sed s and we will use pear to mango inside our fruit file. Now, I am already inside my Unix files here. Inside my user directory. We have a file of just fruit there. Now, notice that I did not put the pipe here. I certainly can. That still works too. This is different from tr. In tr, you had to put this there. with sed, you don't have to. It will take a second argument, which is the file that you want it to do, and then we can take that and we can pipe that and we can have something like mango_fruit.txt.
Now, we've redirected the output into this other file. Now, notice here that we did not use the g modifier. We did that, we didn't use the g modifier, but at the same time it replaced pear here and it replaced pear down here, two times. Why is that? It was because each line gets treated as a stream. Let me show you an example where you can really see this in action. s/a/x and we will do that with the fruit file. Now, notice here when we had, for example, banana, it only changed it the first time.
It didn't change it the second time but it did do it on the next line. It found the first occurrence and then the first occurrence and so on. But then it got on papaya. It only used the first one. We didn't do it globally. If we switch that and you do the g in front of our global, now notice the papaya all got changed. So, it's line by line. So when we say global, we are saying per stream and each one of these lines is being treated as a new stream that's coming in. Now, you can search for just about anything inside a file and replace it. It's useful for things like let's say we had a file that was written in Britain.
So it has the word colour in it, spelled with o-u-r. We want to change that to color, the American spelling without the u. Then we put that global and whatever file we want to work with. Now, obviously, I don't have the word color in there. That's the kind of thing you could do globally to a file and sed would make it easy. You can also provide multiple sed commands. Let's say for example that I have a phrase like echo. During day time, we have sunlight and let's take that and we'll do sed.
We want to first do s for day and night. That will simply replace the day with nights and that says during nighttime, we have sunlight. If we also wanted to replace sun with moon, we can provide a second command and the way we do that is we put -e. It's important that it's lowercase and that says that we are going to have multiple commands. We are essentially going to build them up. So, -e in the first one, -e, and then next sed command, s/sun/moon. There we go and now it does both of them.
So, all you have to do is prefix it with that -e option and it says "Oh, what comes next is an argument to -e. Therefore, I am going to take that as one of my sed arguments and I am going to wait until I get another -e." And I will take that and just sort of build up the set of filters that we want to apply. So, it's nice. You can actually put together a five or six of these and change a whole bunch of things all at one time. And you can remember that it's the e because e is for edit. So we are providing the edits. So each one of these is an edit that we want for our stream editor to use.
Now, we won't go over it, but if you have a lot of sed commands that you want to run on a single input stream, what you can do is you can put all these edit commands into a file and you can use the f option to run all of them at once and that will run every sed command that's in the file. So you can have a set of regular things that you want to change, sort of filters you want to apply. Save them to a file and they're available to you. Anytime you want them, you can just call up that sed file and say hey, run all of these commands on this file for me. Now, these will give you some examples of just the simplest substitution that you can do just by using literal characters.
Where sed really becomes powerful was when we start working with regular expressions and that's what we will look at in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97212 Viewers
61 Video lessons · 84503 Viewers
71 Video lessons · 68706 Viewers
56 Video lessons · 101188 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.