Updating File Contents
00:00 Okay, so, and since it has a new filename now, let’s also change the content of it. So can you quickly show what it contains right now?
Yes. Currently we have
"Hello, Terminal!". Okay, so to fit the filename, I want you to change this one into
"Hello, World!". Yes. And I guess we have a couple of ways now that we’ve seen that we can do this.
And just as a reminder, if we were go back and do something similar like we did earlier, I could do
World" like this, and if I just do with a single redirection (
>), then that would do the job. So this is one way to do it. I can just overwrite what’s already there. Also, just to have a little peek at the power of the terminal, I think I want to show a different way of doing this. Okay.
So far I’ve done nothing. We still see the contents right there. There is an old pattern language more or less, that’s called
sed, which means stream editor.
01:06 And what I can do with this one is that I can just say this little magic.
01:12 Let’s see. Okay, you’re currently writing something that looks like a path to me, but what is it exactly? Yeah, so this is a pattern command, essentially.
And we can see what happens if I just run this on
So you could see over here,
hello_world still contains
So what I’m saying with this
sed command here is that I want to substitute, so the
s there, what I have there with this. So I want to substitute the word
World. Okay, so it says
sed and then a space and then
World and then a
/ at the end to show that, like, this command is—or this pattern—is ending now, and then you define the file
hello_world where it should happen. Yeah.
So this allows me to just substitute the
World inside of this one, and this I can now do on many files if this is something that we wanted to do.
I could just change it all over the place. So it would be a very effective way of changing things. Okay, but then it outputted the
"Hello, World!". But interestingly enough, when you showed me the content of
hello_world, it was still this old content with
"Hello, Terminal!", so it didn’t save anything yet. Right, and this is one of the places where we could really go bad because now I would kind of think that I want to send this back to
hello_world.py, and this will not work for us. Okay, what happens if you run this now? Let’s check. So if I run this, it seems like it worked, right? Nothing bad happened … except that the
hello_world is now empty.
03:00 Boom. Okay. And so this is a great way to just lose all your hard work that you’ve been working on, and that just has to do with the order that this command is running.
So it starts by preparing this file that we’re sending stuff into to be written, and then it starts actually running the command. But by then,
hello_world is already empty. So
sed takes the empty file, replaces all Terminals with Worlds, and writes that empty file back to
hello_world. So what we really wanted to do here, let’s see if we now just go back to having a proper
hello_world that says
So let’s re-create all our hard work like this. So now we’re in the setting where we have
We saw that I could run
sed like this. So currently you are using the
↑ to traverse your history to to quickly access any commands that you entered before. Exactly.
04:11 And I was also seeing—just a quick detour—that you were clearing the line, so that you had something written, like you have right now in the line, but then you did some magic, which I didn’t see, and it was a clear line.
04:23 What did you do there? Yes. So you can always do Control + A to jump to the beginning of a line, and then depending on where you are on the line, if you do Control + K, it’ll just cut whatever’s at the end of it.
So if you do
Control + A
Control + K together, that kind of just clears the line for you. Cool.
But yeah, back to our little final thing for
sed here, so now we tested essentially our pattern here, seeing that this does the job that we’re looking for.
So now I just want to store the results. And the way that’s done is to—let’s see here—use the
-i option, so essentially run
sed in place.
And if I do this and look at my
now it has changed. Let me just point out, though, that now we’ve kind of stepped things up quite a lot here, and most of the time, we can just work with
cat to kind of manipulate files like this.
Starting to look into
sed is something that might be useful if you suddenly have a lot of stuff that would be tedious to do manually. In general, just running things with
cat is the easy way to do with this.
Okay. But I mean, with this
sed command, one thing to keep in in mind is if you are not using the
-i flag, then you get basically a preview of what would happen.
So if you only would’ve written
sed and then
s/Terminal/World/ and then the final name,
hello_world, you output how your file would look like if you would run this command with the
-i. And with the
-i, you’re actually writing it to the file as well. Yes. And this is kind of consistent with how the terminal works in general, that it has something called standard input and standard output, where standard input typically is your keyboard, and standard output typically is the terminal screen, and input kind of goes from standard input, and output goes to screen, and then you need to take some action to put it somewhere else, essentially.
Become a Member to join the conversation.