 Alright, so here's a very important optimization if you want to feel superior to people on the internet Or, I mean, at least if you want to write your scripts very efficiently and optimize and stuff like that This came up in a video a little bit ago And I realized I didn't have enough time to explain it just in case you don't know it So, here's a video on it So, I did something, we were doing something like this And, oh, actually, this is the lesson of this video Never use a complicated tool when a simpler tool exists Never rely on complex technology when simple technology exists That's a recurring theme on this channel But, so, let's say we have a variable in our shell We have, and all it has is two Well, it's one string, it has two words in it Apple and orange What if I want to do something with that information? I want to, you know, print out just Apple Or print out just orange, okay? I used these weird commands in a video previously to do stuff like that in a script And I said that that was more efficient than alternatives Now, first off, well, I'm going to explain how these work Because they look like totally vexing But, first, what do people usually do when they have this issue? Let's say that you have, let's do it ourselves Let's say we do have this variable, Apple, orange So, of course, you can print a variable out Now, right off the bat, a lot of times people will use something like AUK Let's say we only want, so we have this variable that has Apple orange in it We only want to print out Apple A lot of people will do this They will say, okay, I saw someone do this, so I'm going to imitate it And it does work, I'm not knocking it, I've done it before, I do it But, let's say you pipe it into AUK And AUK has this option where you can say, okay, only print the first column So you run that, and it only has Apple Or you change this to two And it says, okay, I'm only printing orange That's how that works So, this is how people often do it Of course, I just ran it here, it was nice and quick But this is actually really inefficient, okay And what I mean by that is every time you run this, you're calling AUK Now, first off, let's, let's, we've got to bust out time If we time this, you see that, you know, here's the time it takes Now, of course, that's very quick, that's way faster than humans are going to notice But, I was writing a script last week where I was looping through something And I had to do exactly this, this modification And because I was looping through something and running AUK Calling the separate program AUK over and over again It was so laggy Whereas, let's say, we just do this Okay, and let's say we time this How quick, how quick is this command? Oh, look at that, it is like 0.001 Okay, actually if we rerun it a lot of time, I've run this before Sometimes it's literally just 0.00 This is infinite, this is one-sixteenth of the time Of the AUK command So if you're running this in a script, you definitely want to be doing this Simpler alternative that's built into the show Actually, let's have a three-way comparison Because a lot of people use AUK A lot of people will know that AUK is actually a very complicated program It's very big, it does a lot of things The assembler alternative is usually cut So we said AUK was, you know, let's re-time AUK Where did that go? Okay, so AUK goes about that fast It's a little faster this time, but still pretty slow Let's try cut Okay, I'm going to say cut, use the delimiter of spaces Because we have spaces separating the columns Quote-unquote And format for me the second column Okay, so AUK took around this much time Cut was nearly half as fast Twice as fast with half of the speed And if we use this built-in equivalent, it's way faster So we want to figure out what this actual built-in equivalent is But look at it, it looks confusing, so let's explain it So I have this little script here, it's called Tester Just to validate that it works This first command will print out Apple This second command will print out orange And they get it from this variable No fancy, nothing else fancy Runs extra quick Okay, so let's figure this stuff out Okay, I'm going to actually just sort of get rid of this stuff by exiting before it So of course you know that we can echo out a variable Okay, that's everyone knows that kind of stuff It's not run time on that because that's going to get annoying So, and that'll echo out Apple orange Let's say you can also do this You can also modify a string and do special stuff with it Add on special things, here's an example I'm going to say pound sign and then I'm going to say A Okay, what does this do? If I run Tester, what that has done is it deletes that first A Now what the pound sign does is this It says delete, I'm going to give you a substring In this case our substring is A I want you to delete that from the beginning of this string So if I say A it deletes A, if I say app It's going to delete app, if I say Apple It's going to delete Apple Notice it did not delete the space here, it just deleted Apple What happens if I say orange? Okay, here's a good question Nothing happens, it prints out the whole variable Because pound sign is only deleting from the very beginning If it doesn't find a match at the very beginning It's not going to delete So if I say orange, or if I just say or It's not going to delete anything But you can also match globs So I can put an asterisk in here And it's going to try and find the match of an asterisk The asterisk of course means in glob matching It means any character So it's going to say, okay, I'm going to start at the beginning I'm going to look for any characters I find And then I'm going to find OR And that's when I'm going to stop deleting Okay, so if I run this now It is going to delete all the way to OR Or specifically it's going to find this whole regular expression Of dollar sign, or dollar sign Asterisk OR And then it's going to delete that, okay So it's deleted all the way to OR Or if we change this to L What's it going to do? It's going to start at the beginning It's going to find the match of the glob And then L and it's going to delete that All from the beginning, okay So that's what that's doing Now here's a trick, this is a trick question What happens if I just have a glob? Okay, what do you think? Now normally the glob can match literally anything It can match nothing, it can match anything So what is it going to do? Is it going to delete nothing? Or is it going to delete anything? Okay, in this case It's going to delete nothing Now if you have one pound sign What that means is I want Glob match conservatively Glob match as little things as possible As few things as possible So if I put two pound signs as I have down here That is going to say, okay When you see an asterisk, be really greedy Steal everything, get rid of everything So if I run this now, it actually says Okay, the whole thing matches the asterisk So I'm going to delete it all Now in some cases of course Let's say the example of asterisk P If I run that with two pound signs That's going to delete both P's Because it's going to say I'm going to be greedy I'm going to take, I realize that this matches You know, I'm going to be really greedy I'm going to take as many as possible If I have two pound signs So I'm going to delete APP But if you have one pound sign You're saying be as conservative as possible In that case, it's only going to match It's not being greedy The asterisk is not greedy So again, what pound sign does Is it deletes from the beginning If you have one pound sign Your globs are going to be conservative If you have two pound signs So what do you think that the percent sign does? Well, let's try it out Let's see what this does So let's change this to a percent Oops, so we know that If we run A In pound sign It's going to delete A from the beginning What happens here? Well, nothing happens here Well, that's because percent sign Deletes from the end So if I change this to E You will now see that E From the very end has been deleted Without it's E So we can also put in Here, we can put in And you see that it's now Apple Aura Or we can of course Match globs as well So we can put in and then Glob match And that's going to delete In G E Or let's actually try again with P Because that's a good example of the double letters In this case Since we have one percent sign Just like in the case of pound signs If you have one percent sign It's going to be very conservative in what it deletes So its glob Is only going to match up into the first P And delete only that Whereas if you have Two percent signs Exactly equivalent to Having two pound signs It's going to delete both P's The glob is going to be grittier So that's what's going on here Now So just as a rehearsal This is a much quicker way At looking at strings Deleting portions of them Or getting subsets of them This doesn't work the same way as Awk when doing multiple lines But if you just have simple variables Like this it is much more efficient Much faster It happens basically instantaneously You should always do this if you have the opportunity It is a built in solution for your problem So just as a reminder Pound signs delete from the very beginning Maybe we should just rehearse So pound signs delete from the beginning If our string is Apple orange I can put in Apple And that is going to delete Just Apple, it's not going to delete the space here If we want to delete the space We have to delete that Whereas percent signs Are going to delete from the end So it's not going to delete anything here Because Apple space is not at the end But if we replace it with Space orange What's it going to delete? It's going to delete all of that stuff You can of course put globs in these As I said And if you double them the globs are going to be greedy Now one final note Let's look at these commands that I already have Now I should say Let's just run these commands And you'll see that I get Apple and orange So think about it If I'm using two percent signs here And two pound signs here If I only have one of them In each In this case it's actually not If I run them over again It's matching space And then glob So the reason this deletes space orange Is because I say here's a space And then here's an asterisk Delete everything there Now the difference happens Let's say we change our variable Let's say our variable is now Apple grape orange Only one glob matching right now So let's say we're being Conservative in our glob matching If I run this This first command is going to delete All of this because it's matching Conservatively And the second command is going to delete With the pound sign It's going to delete this without the G I didn't mean to highlight that So in both cases Grape is in them Whereas if we put in the extra symbols To tell it basically be greedy Or in their globs It's going to not print out grape Because it's actually matching Just to be clear Let's say this one here If we tell it to be conservative It's only going to match up to this space But if we tell it to be more radical It's going to be up to this space That's the difference I re-explained that a couple of times But that's how you do it Edit substrings This is built in the POSIX shell Or at least Dash There's even more of this kind of stuff with Bash Maybe I should do a video on that Alright, so that's enough This has been gone a little longer than I expected But I'll see you guys next time