 One of the most important command line utilities on Linux is the said command. What is the said command? Well said is short for stream editor. It is a stream editor that allows you to filter and transform text. Essentially what this can do is it allows you to search for strings of text or a specific pattern and then replace that pattern with whatever it is you tell said to replace it with. So it's really kind of like a search and replace kind of function and You'll use it all the time once you discover the power of the said command You'll use it all the time both at the command line interactively and also in your shell scripting So let me switch over here to my terminal and let me go through some of the basics of said Substitutions because that's probably 99% of what people do with said is they run the basic Substitution command which goes something along the lines of said and then inside single quotes You do s for substitution slash and then a string that you're searching for So I'm gonna do slash find and then slash replace and then the ending slash and then the ending single quotes So we're telling said go search for in this case the word find and when you find it replace it with the word replace And what this does is it searches every line for the first instance of the word find So it's not gonna replace every instance of fine only the first time it appears on each line And then what you could do is we need to tell said hey, where's the input coming from well You can tell it hey take it from the name of a file So use the less than sign which is the left pointing Chevron So if there was a file on my system called old file That's how that would work and then we need to tell said where to write the output Well, we can tell it to write it to new file using the greater than sign the right pointing Chevron And if you guys want to see a real-world example of that command What I'm gonna do is I'm gonna tell said to read from my x-resources file I want it to go and search for the first oh on every line the first lower case Oh, and then I'm gonna replace it with a capital O and then of course I want you to write the output to a new file I'm gonna call it said dash test I'm gonna run that command and if I vim said dash test What you will notice is this looks like a regular kind of x-resources file except the first oh on every line For example, the first oh on this line is in mononoke. It took the lower case. Oh and replaced it with a capital O It didn't change every oh to a capital O. That's a different command. I'll show you that in just a second We just replace the first oh, but sometimes you do want to replace every oh on every line, you know With something so how you would do that is you do s for substitution And then what you're going to search for lower case oh in this case And then what you're replacing it with capital O in this case and then after that last slash do g and that specifies that this Substitution is a global substitution do this to every instance of oh that we find if I run that command And then once again, I'll open said dash test in vim Every single oh in this document now has been changed to a capital O now That's a basic example of how said substitution works You're not often doing this as far as taking input from a file and then taking the output and writing it to a new file That this was just an example probably most of the time what you're doing with said is you're taking the output from other Shell commands and piping it into said for example if I did echo Derek here So I'm going to echo Derek and then I'm going to pipe it into said and I want to replace the first instance of Derek on every line with DT and And that's how that would work now another interesting example would be something like this echo and then I'm going to echo this sentence The emacs file manager is dear Ed. I'm going to pipe that into said I'm going to do a substitution We're going to replace red with green and now it reads the emacs file manager is die green Because it didn't search for the word red, you know and replace it with the word green It's looking for a specific string of characters anytime Red appears in the text is going to replace it with green So if you really, you know did only wanted to replace the absolute the actual word red You probably are going to want to you know, maybe add a space, you know in front of it Of course it you're still going to get some false positives because you know a word like reddit For example would also have the beginning of that word Transform, but you know at least in this case it would leave dear Ed alone if I added red to the end of the sentence here Would that work? No, it wouldn't because it's going to change a dear Ed to dear Ed green And it actually replaced the space here too. So, you know, this is just some things you have to take into account here Sometimes is you know, sometimes you get these consequences that you really didn't anticipate So understand that the said substitution is searching for strings. It's not searching for words And now let me clear the screen now earlier I had showed you guys how to take the said command and redirect input from the old file and then output to a new file Well, what if you just want to take a file and change it and then write to that same file? Well, you can do that with said to what you need to do is you need to use the I flag So do something like said dash I and then inside single quotes for example, I could do the word find again That's the pattern we're going to search for once we find that we're going to of course replace it and then we'll do a global Substitution because most of the time honestly when you're doing substitutions and said you're probably doing the global substitution And then finally just give it a file name. Don't worry about, you know Directing it or wherever, you know, don't worry about the greater than or less than signs or anything Just give it a file name and said dash I means hey go search for this pattern replace it and then write to the file Now if you want to see a real-world example of this what I could do is run this command here said dash I because we're going to write to the file once we complete the substitution and I'm going to do s Slash Taylor because I know in my bash RC I have my name in the header of my bash RC My last name is Taylor and I'm going to replace it with Tyler. Maybe maybe it's a mistake Maybe maybe Taylor was really supposed to be Tyler and then slash G for a global Substitution although I think Taylor only appears once in the document But the point of this is to show you that when I ran that command it actually did Write to the bash RC if I did a VM and I did my bash RC you see in the header My name is no longer Derek Taylor now. It is Derek Tyler now of obviously I want to go back and change that So let me go back in here and change Tyler to Taylor Run that again and this this time when I do my bash RC here and them you see my name has been Put back to the correct form. So let me quit out of that and let me clear the screen once again Now sometimes you want to tell Sid to only go and search and replace something on Specific lines that match a certain pattern. So hey go find this pattern first and then when you find this pattern You know, that's the line that I want you to run this other pattern on I hope that makes sense. Let me show you this in action. So let me man said and we'll just do this on the man page now The man page for said is actually quite long. Let me do TL DR said here TL DR is kind of like a man page cheat sheet It's just a really short man page that gives you some of the basics of said but the output from TL DR said Has several lines in here that start with the word replace. So what we could do is let's run the command TL DR Said and then this time I'm gonna pipe that Into said and then what I want to do is to find a line a pattern for a line You just give the pattern right up front. So that's basically telling Sid find every line that has Replace in it and that's replaced with a capital R. That's very important and then slash s for substitute What are we substituting on those lines? We're gonna go find the word the all lowercase THG replace it with capital THG and then of course give it the last slash and the last single quote And if I did that correctly all these lines that have replaced now the word the in those lines will be capitalized and That's exactly what happened. You see it found all the lines that started with replace and then it did a search for THG and replaced it lowercase to uppercase now That wasn't a global substitution that we did if I do a up arrow You see, you know the lines that have replaced it found The in the first instance of the on the line and replace that so if I wanted a global Substitution remember to add the G at the end and now you see the lines that had replaced and had multiple instances of the now All of them have been changed now The next thing I want to talk to you about is searching for a pattern and then just deleting that line because that's something You occasionally will do with said and actually since we've got the TLDR information right in front of us You can actually see how to do that right here And it's said and then in the single quotes slash the pattern We're searching for slash and then D for delete and then file name You know where we're actually getting the input from now I'm not gonna do this on a file because I don't want to start deleting text from some of my important files But what I'll do is once again Why don't we just do the TLDR on said and I'm gonna do a deletion here So what I'm gonna do is I'm gonna pipe this into said and then in single quotes I'm gonna search for the word line underscore pattern because that was actually the line We were just reading fine line underscore pattern and then delete that line If I run that you see delete lines matching the line pattern had the word line pattern We've deleted that line. Now. Let me clear the screen now Let me show you how to do multiple of these Substitutions in one said command because sometimes you want to do a search and replace, you know on a couple of different search patterns How do you do that all in one said command? Well, let me cat out something I'm gonna cat out my Slash Etsy slash shills file because it's such a simple file at all it outputs You know just a few lines here in the terminal and what I could do is let's run a said Substitution so I could do something like I don't know said and then in single quotes. Let's do s let's replace all instances of usr with just you and then Do a global substitution in this case and that's how that would work But say I wanted to find also every instance of maybe been maybe I want to change that to just be That's not gonna work because what I need to do is till said I need to give it this dash E flag in front of the first command and then the dash E flag in front of the Second pattern that we're searching for and now it will actually go and find every instance of user and been and replace those with UNB now one thing with the said command because it uses slashes as the separators Well, sometimes the string pattern you're looking for has slashes because maybe I wasn't looking specifically for user You know usr. Maybe I wanted to search for slash usr slash been slash You know and there's slashes as part of the search pattern and then of course you got the slashes as separators in the said Substitution itself so said is not picky about the symbols that you use for separators in the commands If you didn't want to use the slash you can use pretty much any other kind of special symbol that you can think of as Long as it's not part of the search pattern said knows, you know what you're doing So if I change, you know those slashes to the pipe symbol in that first command, you know It still runs that just fine because it still replaced user with you And the same thing with the end here if I want to replace all the slashes with the pound sign Which pound is probably the second most common Separator symbol for said if for some reason you don't want to use the slashes or you know the search patterns You're searching for involve a bunch of slashes and you need to change to another character It seems like the standard one to go to is the pound sign Now one thing you can do with said is you can print specific lines as far as lines that match a search pattern It's kind of like a grip for example once again. Let me cat out my slash It's these slash shells fall but this time what I'm going to do is I'm going to pipe it into said and I'm going to Give it this in flag so dash in and then I want it to search for this string Find the string usr and then slash p for print and giving it this kind of command What it's going to do is it's going to print the lines from slash Etsy slash shills that involve the string usr Let me clear the screen one thing I want to do is I want to show you guys some tricks You can do with said because I think a lot of you guys watching this video probably know some of the basics of the said Substitution, but sometimes you run into weird things that you need to do for example What I'm going to do is I'm going to go to the end of this line and I'm just going to start adding some spaces I'm going to go. I don't know to the end of this line here and let's just start adding some spaces I've got some unnecessary spaces here and a lot of people don't realize You know one of the most powerful features of said is to go and delete these extra spaces at the end of a line And you can actually do that with said so if I paste this command here And what this is going to do is said dash I because we're going to rewrite this file Right, it's going to go and we're going to do a substitution slash and it's going to search for space Asterix dollar symbol and what it's going to do is it's basically going and searching every line that has spaces at the end of it Right, so just the last characters or spaces and then it's going to replace it with nothing That means it's basically deleting that and then of course the file We're going to do this on is this test dot sh It's just a random gibberish file that we're going to play with here And if I did this correctly and then did vm test dot sh Now these lines now should no longer have spaces at the end. Yeah, if I go down to this line Yeah, there's no longer anything at the end of it now another interesting thing you could do Let me do some tabs I'm going to add a tab at the end of that line because that's another thing other than extra spaces Sometimes you have unneeded tabs, you know in a document How do you get rid of the unnecessary tabs at the end of a line? Well, you do this in a very similar kind of fashion So once again said dash i because we're going to rewrite this file And then we do the s substitution and then slash and then Two opening brackets and then colon space colon and then two closing brackets And then the asterix dollar symbol and what this is essentially doing you can think of all of this here As being a tab essentially so it's going to search for tabs at the end of the line And replace them with nothing because there's nothing in between those last two slashes So let me run that and then if I go back into test dot sh and go to these lines This line here had a tab at the end of it before but there's nothing at the end of it now This one here if I go to the end of this line There's nothing at the end of that line except you know just that question mark So no extra tabs no extra spaces another cool thing that said can do is it can delete empty lines? Sometimes you create these documents and you have these unwanted blank lines in it And you know, how do you go in and get rid of like a million empty lines? Well said does this very quickly for you? So let me do a cat on test dot sh because I believe I did have some empty lines Yeah, there's an empty line. There's an empty line. There's two empty lines here So what if I till said to go and get rid of those empty lines? Well, I can do that Let me pipe this into said and this time what I'm going to do is till said to find Well, first I'm going to do the carrot symbol. That's the beginning of the line Then the dollar symbol is the end of the line So find every pattern that matches the beginning of the line and the end of the line And there's nothing in between it. So that means that line is empty So find every empty line and then slash d remember d is delete So let me run that and then of course all the empty lines are now gone in test dot sh Actually, it didn't write to the file. That's just the cat output that we piped into said And let me clear the screen another cool thing you can do with said is you can replace All the lowercase characters with uppercase characters and vice versa So what I could do is cat out my test dot sh again and I could actually run this command here What this command is it's another said substitution. So said and then s substitution And then what I've got is the brackets a to z find every lowercase a through z And then slash and then backslash capital u and and what that is saying Hey, all these lowercase a through z's make them uppercase That is what those three characters there are basically symbolizing slash g I'm going to do a global substitution on the test dot sh And when I do that you see we replaced every character every lowercase character with uppercase now I could go in here and do the same thing except in reverse We could do something like capital a dash capital z. So find every uppercase a through z And then instead of backslash capital u do backslash capital l for lowercase ampersine And then slash g we'll do a global substitution And now every single uppercase character that was in this document has now been made lowercase You won't find an uppercase character anywhere in that output Probably the most common usage of said other than the substitution command is basically using said as a replacement for the head command So what you could do is you know head dot bash rc for instance If I don't give it any other numbers what head does gives me the 10 Lines the 10 opening lines of that document and prints it out Well, you could do that with said except a lot of people like this particular mean here said 11 q You know bash rc and that gives me the first 11 lines of my bash rc because you know Why not get one extra line with said because that makes it so much better than head right? You get that extra line for free, but I've never liked using said for this I've never really liked using head for this either you guys know i'm an awk fan So what I would do is I would run this command here I would run awk and then inside single quotes n r That's the number of lines less than 13 and then in single quotes dot bash rc And what this is going to do is it's going to print all the lines less than line number 13 So that gives me the first 12 lines of my bash rc. So said 11 q It's really just a poor man's awk n r 13 Now before I go I need to think a few special people. I need to think the producers of this episode I need to think absi gave james michael west a commie allen choked david dylan gregery urion alexander paul Polytech scott steven spin west and willy these guys. They're my highest tiered patrons over on patreon without these guys This episode about the said command it wouldn't have been possible The show is also brought to you by each and every one of these ladies and gentlemen as well All these names you're seeing on the screen right now These are all my supporters over on patreon because I don't have any corporate sponsors It's just me and you guys the community if you like the work I do consider subscribing to distro tube over on patreon. All right guys peace Even on my said videos I use awk