Updating File Contents
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.
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 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.
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.
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
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.
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.
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.