 Hello and welcome! Today we are going to be continuing with our shell script tutorials and today we're actually going to be looking at a tool called grep. It is not built into your shell, it's not part of bash, which is what we're primarily looking at. It's an external tool but it's a common that's one of those core tools that's on pretty much all systems. If you have even a small little device like a router or modem and you have a shell on it and things only got like 8 megabytes of hard drive space, it probably has grep on there. Grep is a tool you'll use all the time in your shell commands. So it does a lot! I'm going to show you the very basics but in general it just it grabs things, it looks for matches in files and returns them for you. So let's go ahead and jump right into that. I am in an empty directory here. If you remember LS will show you, will list what's in your directory. I have nothing in this directory. I need to create a file. Now you can use your text editor. I like using Vim and I will do some videos in Vim in the future. But use whatever text editor you're comfortable with. But I'm going to show you a cheap little trick here. We've learned about the cat command which will show you what's inside file. We'll concatenate files together. But if you say cat and then you use your greater than symbol, you remember that in previous videos we worked with this and when you use that it would take the output of one command and put it into a file. So I can call this file.txt for text, right? I'm going to hit enter. It doesn't bring me to prompt at the cursor. It's just kind of there. So what I can start doing is I can type stuff like my name is Chris. Your name is John. Banana. This is fun. Let's get going. I'll hit enter. I'm on a blank line here. I'll hit control D. I just used cat as a what they will say poor man's text editor. If I list out files now you see that I have a file here called file.txt which is what we created right here. If I use cat now to show what's in that file you'll see everything we just typed. Now if I wanted to add a line I can do the cat command. If I was to do this command again it would override that file. But instead of overwriting it we're going to give it two greater than symbols which means append. Add to the end of this file. Now if I type something like berry and blueberry and then again to finish it off we hit control D. Now if I was to cat out what's in that file we can see that we've added to the end of that file berry and blueberry. Now I can hit control L or I can type clear to clear my screen. I am still in that same directory and we have that file. So what grep will do for us we're going to search for stuff. So again if I look at what's in this file you can see that there's multiple lines. I'm going to go ahead and grab any line that has the word Chris in it. So I'm going to grep for Chris. And also on most Linux systems if you want to copy something the easiest way in the terminal is just to highlight it. And then on your mouse center click which if you have a wheel is just clicking the wheel down and that will paste whatever you have recently highlighted the last thing you've highlighted. Now I'm going to say grep Chris and then I got to give it what I want to grep through. We're going to look at that file and it will return only the lines that have Chris. If I want and again I can go up and down arrows to go through previous commands I am going to go back here and I'm going to say only show lines with the word is great. So now we've learned how to look for lines with matches. Let's say I want to find all the lines that have the word is on it and the word Chris not necessarily next to each other. Well I can always pipe the output of any command into grep. So I say the pipe symbol which is shift and on most keyboards it's going to be I don't want to say most keyboards because keyboards are different around the world but on a standard QWERTY keyboard is shift and the key that's right above your enter key. It's that straight up down line. Now I can type grep and I can type in Chris and now it's going to search for lines that have is in it and then it's going to search for lines from that output that have Chris and will only return that line which is only one line that has christened. So if I was to cat and append to my file and again I can start typing a file name I can hit tab and it will auto complete for me so you don't have to take out the whole file name. I can type in Chris is cool. Well that still has Chris in it. Let's just go Chris. All right let's do films by. Oh I just realized this is a bad example because the word is isn't my name. Okay we'll still do that and then we'll do John Smith and then again to the end control D. Now if I cat out what's in file we have all these lines here again control L will clear the screen up arrow to go through our previous commands. Let's look for lines that have the word John on them. We have two but let's say I only want lines that have John is and not necessarily in that order I can say grep is. Now let's say I want to find lines that have John Smith. Well what I I mean we don't want to do this necessarily because let's add something to the bottom of our file. So I'm going to say Terry Smith again control D. I can show you what's in that file. Now up arrow if I was to do this I'm going to get lines that have just John Smith but again bad example for what I was trying to do. If we add another line to our file John is a Smith. How about that? Control D. Now we can grep for John and Smith and we'll get both the lines but let's say we just want the line that says John Smith. Well since there's space in there what we can do is put this in quotations with Smith and even if you don't if it even if it's only word one word it's good practice in my opinion to put things in quotation strings and quotations. Now let's say I want to find all lines that say John and all lines that say Chris. Well if I do this I get lines that say John. What I can do is add an E here which says hey I'm looking for the next expression. Well we can add multiple expressions so dash E and I can say Chris. Now it's returning lines that have John and Chris as opposed to doing something like this and then grepping for Chris. Now I'm going to get nothing because I'm not getting any lines that have both John and Chris on it. So here we're getting lines that have either John or Chris and here I'm getting lines that have John and Chris on the same line. Now by default grep is case sensitive so let's go ahead and cat and add into our file text. I'll just say John is also cool. I control D now again I can cat out that file and you can see we have John is cool and I can grep for John from file and you can see I got John but only John's with capital J's. If I come back here and make this lower case J it's going to show the lines that have John with a lowercase J. Well if we want both we can say dash I which means case and sensitive ignore the case. Now it's going to return all lines that have the word John on it even if it's lowercase or uppercase and it doesn't matter where those cases are you can have the whole word capitalized or whole word lowercase this is going to ignore cases all together and again grep does a lot of things but I'm going to show you just one more one or two more things let's say we want lines that don't have John on it okay we want all lines that don't have John I'll do V here now it's going to return all lines that don't have John with a lowercase J see so we still get lines with John with the capital J. If we do this we'll get lines that have John all lines that don't have John with a capital J so we still have the lowercase John here now if I can do this I can do say case insensitive so don't look at the case and show V means the inverse of this so now we're going all lines that don't have John in it and we're not modifying the file at all here right so if I cat out file you can see all that stuff still in there but if I wanted to save the output of that I can create a new file I'll call it file 2.txt and again the extension is just for you it's a text file regardless of whether you have that on there or not now I can list out you can say I have two files I can cat file.txt and I can cat file 2.txt so we're learning a lot with grep and this is this is a great thing again now a lot of people will do something like file.txt and then put that into grep and then grep for John and that works and there's not not problem with doing that when you're working at the shell when you're writing a script you probably don't want to do that it's preferred to do the grep John and the file why is that well it's one less command it's one less process to run but it's good to know that you can pipe because you might may end up piping it's not a big deal some people will make a big deal about it it's just one less process and if you're looping through a lot of things that can save you time if you leave out this extra command but when you're working at the shell at last times you're just stringing along you're running one command sing the output okay now I'm gonna pipe it's it'd be silly to go back like if I was to you know cat out file.txt and then go oh okay now I want to go find Chris now I'm gonna type a whole new command well no it's quicker just to go up grep Chris right and then all I want lines to have cool I'm gonna go grep cool so if you're working at the shell to go like this and go okay now I'm gonna go back and rewrite this it's it's it's a matter of opinion it is more efficient not to pipe as many times as you can but it's also the power of the shell is piping but just be aware of that because if you do cat and then grep and that's all you're doing people will give you a hard time I wouldn't give you a hard time I might mention to you it's better if you don't so don't take it you know if you come and ask me a question I see that you're doing that I'm gonna recommend that you don't but some people will just jump down your throat about it and just don't worry about it people who do that again take advice is advice ignore people who are mean but that is just a quick look at the grep command now again this is the grep command I should have also mentioned there are lots of different versions of grep there's p grep e grep and they all work a little different in fact the when you run grep on your machine it may not just be regular grep it might be p grep or e grep and or some other version of grep and there are differences but in the 18 years that I've been using Linux and I've been use writing shell scripts I can't think of a time where I've ever had an issue where I'm working with grep and it doesn't work like I think it does and there can be there definitely can be but just be aware of that because some people might give you again a hard time if you say grep but you're actually using let me let me see something so there's a which command this little bonus here if I say grep it's gonna tell me what where my grep command is and if I list out and list with dash LHA this gonna list it with some more information and right here it's sometimes things will be alias pointing so I might have my grep command but it might be pointing at another file so I was just looking to see if my grep was pointing to another file but that's all I have for you today we're gonna be using grep in a future video that's why I wanted to touch on it today and it is a tool because when you're working with shell scripts you are going to get the output of commands regularly and you only want parts of those commands so or the outputs so you're going to use grep a lot or looking through files it is one of the most common tools and I just want to show you the basics of it and it can get complex where people start using regular expressions which are very powerful but that's a whole another topic I do thank you for watching please visit filmsbychrist.com that's Chris the K there's a link in the description check out all the links in the descriptions video because there's ways to support me I have patreon paypal Libre pay pay blah blah blah buy me a coffee I would love support if not be sure to like share subscribe and comment let me know what you think and I hope that you have a great day