Linux Essentials - The sed Command

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today's video is proudly sponsored by lenode the note has been doing cloud computing since 2003 which is actually before amazon web services was even a thing on the notes platform you can get your server up and running in minutes and they include all the popular distributions such as debian fedora ubuntu and get this even arch linux and let's be honest what could be better than a linux focus cloud server provider that lets you tell all of your friends i run arch the note has multiple server plans available to make any app scalable and flexible you could use it to host a blog a vpn server a minecraft server and much more in fact lenode is the platform of choice to host the entire web presence of learnlinux tv in addition the note offers 24x7 365 support regardless of plan size so you can get help from a live person when you need it new users can get started right now with one hundred dollars towards your new account and i highly recommend you check them out because lenode is awesome and now let's get started with today's video [Music] hello again everyone and welcome back to learnlinux tv in today's video i'm going to teach you guys the basics of the said command it's one of those commands that i recommend that everyone learn and it could be a bit tricky for some newcomers but honestly it's not as bad as you might think it's quite easy to learn the basics and that's exactly what i'm going to do i'm going to teach you guys the basics right now in today's video and the basics that i'm about to teach you will serve as the foundation that you could build upon for future learning so let's go ahead and get started now maybe i should have told you guys in the beginning what the set command even does in the first place set is short for stream editor and you can actually use it to filter and modify text it's often used with text files to change things in place inside text files which is one of the examples that we'll go over but there's all kinds of things that you can use the sed command for and we're only getting started in today's video offline i went ahead and created a text file and that text file is going to serve as the basis for the examples in today's video and here it is just a random example i know maybe i'm just hungry i have no idea why i decided to use pizza toppings as the basis for today's video but you know what i did what i did and here it is so hypothetically here are four different topping combinations for pizza now at first you actually might think that there's nothing wrong with this file actually if you look closer you'll notice that there's something extremely wrong with this file pineapple yes pineapple is listed as a topping heck no we have to fix that now of course i could go into this file i can open it via nano vim or whatever and i could just manually change it and to be honest it wouldn't really take me that long to make the changes manually we only have four lines inside this file that includes toppings anyway so it might only take what 30 seconds now imagine if there were thousands of lines here in that case it's not really going to be all that convenient to do this manually let's see what the sed command can do for us so what i'm going to do to show you the first example is i will type sed and then i'll type single quotes inside the single quotes i will type s because i want to search for a string and then slash pineapple slash and then i don't know let's go with feta and then what we need to do is give the sed command a file that we want to use for input and i'm going to use the toppings.txt file that i showed you guys earlier and take a look every occurrence of pineapple has indeed been changed to feta it worked video over you could go on about your day it was that easy actually it gets a little bit more advanced from here let's continue on a bit so one thing that didn't happen is that the file itself didn't get modified so what the sed command did here when we executed it is it used the toppings.txt file as an input file and then it executed this find and replace against every occurrence of pineapple and it changed every occurrence to feta now what happened here is that it didn't actually overwrite the file it just well gave me the contents of the file the contents of the file with the changes now what i could do i'm not going to do this but what i could do is i could simply redirect the output to a new file i mean that's fine and it's valid but that's not really what i want to do here instead what i want to do is change everything in place and that's actually very easy we just add one option here dash i allows us to change things in place this time there's no output but we can see that there was in fact output the output was the file itself that's where the output was sent to so we were able to change every occurrence of pineapple to feta inside the file so crisis resolved now already with what i've taught you you could use this command syntax right here to do all kinds of things i mean anytime you want to basically find and replace something with something else you could use this exact syntax right here you just change the word that you're searching for the word that you want it changed to and then you also change the name of the file that you want to make the changes in you include the dash i option if you want everything to be done in place and that alone will probably serve you for more than 50 percent of all the use cases you might use said for again there's many variations but there's also another problem and that problem comes in the form of a delimiter now let's take a closer look at the command syntax that we have right here but what i'm going to do for this particular example is i'm going to change what it's searching for and i'm also going to remove the dash i as well so we've already changed every occurrence of pineapple to feta so the word pineapple is no longer in the file so let's try changing feta to something else so i'm going to change every occurrence of feta to olives and it works but what's the problem though i mentioned concerned about the delimiter what does that mean now when it comes to said the character that follows s is the delimiter so as you can tell then the slash in our case is the delimiter what happens then if i change every occurrence of slash here to a space nothing it still works okay that's interesting so what happens then if i do this again nothing it's fine that's okay that's valid so now i'm going to change the delimiter to a period still fine so it doesn't really matter what you use as the delimiter our original command was this one right here now the forward slash is the most common delimiter that you'll find and i think every tutorial i have ever seen online uses the forward slash as the delimiter that's standard practice again nothing wrong with that until there is something wrong with that so what if for example one of the characters that you want to replace is a forward slash now we can get into all kinds of craziness like escaping things out and workarounds and things like that but it's just a lot easier to actually just use a different delimiter so if for any reason the forward slash is not working for you because let's be honest it's not always going to work in every use case especially if forward slash is included in what you want to replace then it makes sense to change the delimiter to something else and let me try to set up an example of why you might not want to use the forward slash so what i'm going to do is just execute the find command and i'll start the find command in the etsy directory and the type of objects that i want to return are specifically files so dash type f and i'll press enter so that gives me a list of paths what i'm going to do right now is just redirect the output to a file i'm going to ignore these permission denied errors that doesn't really matter and there we have the file and we have the contents now let's say for example i want to remove something and you could do that with set as well you can actually remove something let's say for example i want to remove this right here i want to show only what's after slash etsy there's many different ways that we can do this and i'm not going to get into all the individual ways i'm just going to set up an example where we want to remove something and what we want to remove includes a slash as you see here so how do we actually write a statement that includes a forward slash well you probably already know because i kind of already gave it away but what we can't do i mean that's not going to work very well in fact it doesn't work at all as you can see i'm not really able to use the forward slash here and use it as a thing i want to search for because i'm using forward slash as a delimiter that's a little confusing and specifically what i'm doing is i'm searching for slash etsy and i want to replace it with something i want to replace it with nothing because what i want to replace it with should be in here but it's not as an aside if i go back to our earlier example as you can see i'm able to replace feta with olives but what i'm also able to do and this is what i was trying to do earlier i can replace it with nothing it doesn't look all that great especially right here but you get the idea i can essentially use said to remove something but that didn't work right here because again what i want to remove has a forward slash in it that's going to confuse the command and that's an example of a situation where you might want to use a different delimiter and here's that failed command again and i got to be honest it's a little hard to read so let's go ahead and fix this up what i'm going to do is use a different delimiter i'm going to use a period i'll press enter and it worked on the far left hand side you don't see etsy on any of these lines right here let's recall the command again so we have the said command we're going to search for a string and like i mentioned the character that directly follows the s becomes the delimiter so now a dot as you see here is going to be used as the delimiter we're going to search the file for slash etsy here we have another delimiter after this delimiter right in between these two dots right here we're supposed to type what we want it to be changed to so for example i could just type a random word in there and as you can see it went ahead and put that all the way there on the left but if we don't include anything at all as far as what we want to replace each occurrence with then it's just going to replace it with nothing as you see here essentially stripped that off the left hand side of the file and that's an example of when you might want to use a different delimiter now normally you'll probably want to stick with forward slashes most of the time but again like you just saw you'll more than likely at least eventually run into a situation where you'll need to use a different delimiter when you use said and well be prepared to have a little bit of fun with it so as you can see here i took an echo statement i simply echoed hello but instead of stopping right there and letting hello print right on the screen i pipe that right into the said command replacing in real time the word hello with goodbye the word goodbye is the word that was printed because before i had a chance to even show up on the screen we replaced hello with goodbye as you can see right here so there you go i just showed you guys the basics of the said command and i know this video was a little shorter than most especially in this series but i think it's important to understand the basics because that sets the foundation for the future and now you understand the basics of the said command you can certainly use it any time it makes sense to do so especially when you want to replace text with other text that's the greatest use case right there so let me know what you guys think in the comments down below and i'll see you again very soon thanks for watching [Music] so [Music]
Info
Channel: LearnLinuxTV
Views: 12,024
Rating: 4.9539051 out of 5
Keywords: Linux, Tutorial, Howto, Guide, Learn Linux, LearnLinuxTV, LearnLinux.tv, sed, sed command, linux sed, linux command-line, linux tutorial, linux tutorial 2021, linux tutorial for beginners, course, linux command line, linux for beginners 2021, command line, linux commands, gnu/linux (operating system), linux sed command, linux command line tutorial, linux basic commands, sed tutorial, bash (unix shell), linux (operating system), tutorial, admin, bash, linux, sed (programming language)
Id: nXLnx8ncZyE
Channel Id: undefined
Length: 15min 25sec (925 seconds)
Published: Fri Aug 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.