Transcript from the "Searching File Contents with grep & zgrep" Lesson
>> Jem Young: So find works for finding files but what if you want to do some sort of introspection, you want to look inside a file? We use grep, and grep is, a lot of people don't like it because a lot of people don't like regular expressions. They say if you have a problem and you need to use a regular expression, now you have two problems, which is absolutely true.
[00:00:25] [LAUGH] And a random programming tip. If you are regular expressions in your code, comment what that regular expression is, cuz I promise you while you figure it out, someone else probably won't. And it's just like a tip for just being a good citizen. But grep, we're gonna use some regular expressions.
[00:00:40] We are not gonna go too deep into them because there's probably a course on regular expressions, and they can get really, really nasty. Anybody know what code golf is?
>> Jem Young: It's where you have a problem, and then you post it on the internet, and someone says I'm gonna solve this in the shortest amount of code, and almost invariably someone puts the regular expressions and they get like pages long sometimes.
[00:01:04] And you can have a lot of fun with them but that's not the way I nerd out. I nerd on the Fast and Furious trivia. It's true, I do. For instance, did you know in the first Fast and Furious, they don't have any rear-view mirrors in any of their cars?
[00:01:18] You probably didn't notice that because they never look back. I'm not kidding, okay. [LAUGH] Go on Netflix watch The Fast and the Furious. If I were there, we'll sit aside. But you don't have to, I'm just trying to get warmed up for today. But grep is one of the things that we've used already.
[00:01:33] In fact, we use it when we're typing output through and grep just looks over every single line of that code, and looks for what we're trying to find. And if you want to look inside a file, and this is actually an awesome utility, zgrep search inside of gzip files.
[00:01:48] Because over time, log files automatically get concatenated in gzip. You're here like, what happened December 23rd of last year? You could find that log file, which is gonna be minified, and you can use zgrep and search inside it rather than having to unpack it and then run through it.
[00:02:04] So let's do a quick exercise on grep.
>> Jem Young: And if your node process is running, let's do ps. So to show the running process, we're passing some arguments. aux is show all of the processes. Actually, I have it in my notes here. Show the process for all the users, display the process owners, and then show processes that are not running in the terminal, AKA, processes that are running in the background.
[00:02:30] And you should have a process running because if your server is up and running, the express server is up and running, and that means Node is running. So I'm gonna do that now, clear. ps aux, we got to pipe that with the redirection operator, grep node. And there we go, I have a node process running.
[00:02:53] This is one of these things that keep at the top of your mind if you're ever running through a long, long log file. Grep is the fastest way to find something rather than manually traversing every single line. Let's try do a little, just pipe this, grep. Actually, let me see what's something good to grep for.
[00:03:14] I'll look for every instance that I logged in so I pipe that, grep then jem. Here we go. And here's every instance where I logged in or did something that requires sudo permission. Now, if I was really clever, and I'm not, but if I was much more clever than me, say jem 2.0, I would create a regular expression for everybody that was unauthorized, I can take that list and grab the IP addresses.
[00:03:41] Then pipe those to a banned list. And the great thing is we now have those capabilities of doing that. We could write them to a log file, separate log file, which are like evil IP log file or something like that. And we can do that all in the command line in three or four steps on the command, which is really powerful.
[00:04:00] I know, [LAUGH] we're settling in and I'm starting to get a bit more excited about all the things we're going to learn today. All the things we have learned already.