 In this video we're going to talk about how to do some basic operations on the command line with said and using regular expressions. This is sort of at the request of a lot of people recently just because you know I do all these scripts using all these I guess Unix command line stuff that I guess people have sometimes you know a piecemeal understanding of but it's very useful to at least get into real life implementations things that I've used to understand how to use things like said and grep and awk and stuff like that but in this video I'm going to talk about some of the main uses that you can use said for replacing stuff deleting stuff printing stuff and using again a real life implementation that I've had and also how to I guess get a lot of magic out of it but anyway so we'll go ahead and get into that and as I said this will involve some regular expressions if you don't know anything about regular expressions you'll be fine though you'll figure it out you might even learn something so anyway at a basic level I'm going to open up a file and this the file origin is not particularly important but if you watch my channel you might know that I auto generate shortcuts from a file that looks like this where I have a directory or some here it's a different kind of file on the right and then some shortcut to it on the left and I have a little script that reads a file that looks like this and generates aliases for bash or something like that or Ranger and other stuff but when people started using my dot files they started saying hey it'd be really nice if I could you know say like you know put in a comment or something like this so I'm putting in a comment or have some you know blank lines you know blank lines and comments because originally when I made the script that parsed this file they it only really just assumed that it was already in the correct format and there were no comments for it to ignore or not you know pay attention to or something like that so I want in this video to use said what I want to do is as I said the script that I have is going to read a file that looks like this but I want to give it an input that looks like this that is with comments and blank lines and I want to have a said a said script actually not a said script one single said command one line of said that will change a commented file with blank lines and all this stuff into an uncommented file that my script can use okay and it's going to be very easy but it involves learning some of the basics of said which again are very easy now let's go ahead and get into this so at the basic level I am going to run said on this file BM files now you'll notice here I just gave said in the quotation marks those are the commands that you're running with said in this case I have nothing and I ran it on BM files so this is actually just going to print out the file it's going to show you what it looks like that's all that's all now you can put in the quotation marks one of said many commands and one of the most common ones that we can use is s s stands for substitute it's one of the most commonly used of all of the different you know of all the different seven commands but we'll go into the syntax of it it's very simple so as an example you know let's say so what substitute does as you can probably guess is you find some pattern let's say there are a bunch of C's in this document lower case C's let's say I want to replace lower case C's with capital C's the syntax of this is going to look something like this so you have s for substitute then a slash then the pattern you want to replace then another slash then the pattern you're replacing it with then another slash and at the end I'm going to put a G here this is for other options about the command I'm just going to run this and I'll tell you what G does in a second so if I run this you'll see that all of our lower case C's have now been replaced with uppercase C's so that's what we want in our hypothetical said replace command and this is the basic syntax for it it's pretty simple of course you don't just have to replace single characters you can replace lots of stuff you can replace lower case C with LMAO or you can replace CF with LMAO or anything like that now let's let me now what is the G for now I should be clear let's say let's say I'm just replacing C here now at a basic if we run it with G as an option you'll notice that for example on this line it replaced CFB bash RC with LMA to instances of LMAO if I run it without G you'll see that what that does is by default that is if you don't have G said is only going to run the substitute command once per line so our first C on this line was replaced but not our second one if you want all of the instances to be replaced you got to run it with G so I pretty much always run my said replace commands with G I mean it's probably minutely fat I mean if you know that you're only going to be making one modification per line of course don't use G and in fact it might be faster not to but I find that most of my said commands end up using G but or replace substitute commands so that's that's how we can replace a sequence so what does this have to do with getting rid of comments well it's very easy what we can do is we can replace the pattern of a comment with nothing you can very easily you know just have nothing as your replacement pattern you'll see all the C's have now been deleted from the file so what we want to do is instead of replacing C we're going to replace what a comment looks like so comments pretty much always look like this they're going to start with a pound sign and then we're going to use a little regular expression magic and that is we're going to type in period and then cleaning star so if you don't know what this means it's very simple period just stands for any character and the star stands for whatever the last thing you just typed just say any number of those okay so what this is going to do is actually going to replace a pound sign and then basically every character that comes after it in the line that's what it's going to do so if we run that you'll now see that our comments have disappeared we have no comments anymore this one is totally gone it's been replaced with nothing all the ones down here are totally gone you can see that all the comments have now disappeared from our file now I should be clear said is a stream editor by default we are not modifying the file we are just taking the file as an input and then printing out some output if hypothetically we want to you know change the original file we could run said with the I option but we're not going to do this in this video that because that's not you know it's not what we're doing but just know that's how you do it so don't be afraid if you're not running you know something if you're running said without I it's pretty much safe to run whatever don't worry about it but um okay so I do want to say there's one additional thing we want to add here and that is there are a couple lines if you notice so we've gotten rid of this comment but technically speaking you can't see it but there is a space right before this pound sign now what we can do to ensure that we get rid of all those spaces is we can do the following I'm going to add to this replacement expression slash s and then another cleanie star now what slash s means is any white space character and again the star means any number of those the of those characters so that means it could mean zero spaces it'll match for zero spaces it'll also match for a hundred spaces anyway if we run this it's gonna look exactly the same as it was before but technically it's a little better to do this because if we run it without the this there are technically some trailing white space now that might not matter for a lot of the scripts you're making but I guess it soothes my autism or you know it's more intuitive it just makes more sense I know what I'm dealing with now okay so that that's half of the way there because I wanted to get rid of comments but I also want to get rid of these nasty blank lines in fact let's let's add some more blank lines in because why not make them more visible okay so we're gonna so now we have some extra blank lines so what can we do to get rid of these blank lines now I will say in addition to this little command here we can also concatenate different said commands so I can do if I want to get rid of comments but I also want to replace lowercase c with uppercase c I can do the following after this said command but still in the quotation marks I can type in semi colon and then another replace command to replace lowercase c with uppercase c right and I can run that and you'll see that all of the them have now been replaced okay now I don't want to get rid of the blank spaces I'm not going to be running a replace command or a substitute command instead said has another so said has a way the said's general syntax is give me a pattern and I will look for that pattern in a line and then I'll perform an action on it so let me give you an example let's say how that looks like is so our new command I'm gonna have a command that looks like this and then put a d at the end now what this means is I want you to find wherever c and f appear so c and f appear you know in multiple on multiple lines go to all the lines were c and f are there together and then perform this operation d you can probably guess d means delete so if I run this you'll see all of the lines with CF had now been deleted okay now we can do that as well we can use a regular expression well actually before we even give the final answer just to be clear a said has a bunch of different commands that you can run with this so for example if I run CF instead of D if I run P that printed out a whole bunch of stuff what that did is whenever it find find CF in the file it runs P meaning print so you actually see it run so it prints out the whole document but every time it finds a CF it actually prints that line out twice because we're telling it to to print that out or we could say CF and Q then that means when you find see the first CF you find quit the file etc or we were talking about the other day right so said 11 Q just means when you get to the 11th line if it's not in slashes or if it's just a number not in slashes that's assumed to be the line number but said 11 Q just means when you find the 11th line quit okay so anyway so what we want to do again we know how to delete patterns the pattern we really want to delete is the blank line now of course there is no symbol for blankness but there is a we can do the following we can do carrot thingy hatching whatever whatever you call it plus dollar sign now if you don't know anything about regular expressions I will tell you what this means the hatching Karen whatever it is that stands for the beginning of a line so that is going to match every beginning of the line and the dollar sign is going to match all the ends of lines so when we put these two things together that basically means every blank line because a blank line is just the beginning of a line followed by an end of a line and we're saying whenever you find that on a line delete it we don't want it so if I run this bam that is what we want look how beautiful that looks so now we with one again with one little said command which is technically two subcommands but it's one said command we can take an input file that might have a whole bunch of comments people can comment however they want on this file we will take that and it will automatically turn into this much more parsable document so this is actually what I do now again as I mentioned this is a real-life implementation just a little said command I added to get rid of comments okay very simple now just to give you some ideas for other things you can do and said I mean this is all the technical portion of the video is now over but just to give you another idea of the kind of things you can do with said one thing I have is I'm gonna turn on my transmission Damon one thing I have it up here is I have this little module that I guess I've talked about in other videos a little torrent module and it shows a little emojis based on how many of my torrents are done or downloading or waiting for cedars or seeding or something like that now that might sound like you know you have to download a 5,000 megs of Python modules or something if you don't know what you're doing but really to get something like that you just need a kind of a said command so I'm gonna actually go to the script that runs this so I'm gonna open up my this this particular file and I will show you now what this module is in effect is the input to it is the transmission remote list command which what that does is it just prints out all the torrents you have and it gives a bunch of information about them like how finished downloading they are and stuff and really to get this little module up here all I do is I pipe it in the said and I have a bunch of replace commands so for example really in effect what it's going to do is whenever you know it finds that a torrent is seeding because it says seeding it's gonna replace that with the little sapling up here right so this thing means seeding now notice that I actually have two said commands that just because first I replace them with letters so I can run a sort on them to get them in the order I want then I'll replace those letters with the actual emojis but in essence it's it's pretty simply just me replacing it's me just taking text input and replacing all of that with some kind of more and something more visually I guess intuitive for a status bar icon or something like that so that that's all that all this thing up here it's just a little shell script line well this I guess it could technically be a line just a little line a line of shell script it is really just said replace command commands I'm just replacing sequences with other sequences so anyway that's about it for this video I hope that you've learned a little bit about said if you didn't know anything about it or even if you did know something about it I hope you learned something if you want more videos like this just feel free to say so because you know a lot of people have asked me to do I guess more general scripting videos and hopefully this has been a good example so anyway that's that's about it see you guys next time