 Okay, today we're going to be looking at two different ways of Going through a text file and only displaying lines that have a certain number of words or strings on them We're going to do this like I said in two different ways. The first way is a little bit shorter. It's using awk which Is a great little tool It's actually has its own full programming language if you want to learn it But you can call it from within your shell scripts and and use it That's what we'll do, but then we'll look at a way of doing it with built-in commands Just in case you're on a like a very stripped-down system that may not have awk and you need to do this So it's a little bit longer of command to the second one But should work pretty much all the time So let's have a look here. I have I'm in a folder. I'm going to list out the files We have one file. It's a text file called file dot text I'll cat that out to display the information within it and you can see I have a bunch of just random lines in here with different number of number of words on each line And I'm going to type clear to clear the screen or you can press ctrl-l to clear the screen in most cases And as I said, I'm going to use awk in this first example So I'm going to say awk and I'm going to do a single quotes and then inside that little squiggly braces or curly braces I'm going to say if and then we're going to go parentheses and f Equals equals in this case I'll say three because we let's say we want to get every line that has three words on it No more no less if it has three words. We want to print it So then outside of those parentheses, we're going to say print and Then outside of all that we're going to say the file that we want to search through. So let's look at this real quick We're using awk and then what's inside these single quotes here are basically it's basically an awk script So we're saying we're doing an if statement, but for awk not necessarily our shell. They're going to say nf Which I'm not 100% sure what stands for it's number maybe number something anyway We're going to say the the line, you know, it's basically if that equals three That's the number of words or strings on a line if that's true Then print that line. Otherwise, it's just going to skip over it. So if we hit enter now You can see we got all the lines that have three strings on it I put some special stuff in here just so you can see that it's basically going based on spaces So even though these aren't words it's seeing as three different strings And of course we can change that number. I'm gonna hit up arrow to go to the last command and change that to five and You can see now. It's printing all the lines that have five words or five strings on them Now as I said, that's using awk, which is on pretty much all desktop systems and most servers you want to get into But maybe you're working on something with a stripped down interface a very stripped down shell Maybe you're working in the shell on an Android machine That's you don't have full root access to true root and something stronger or you're working on like a Router those are cases where you tend to have very stripped down shells. So now we're going to use pretty much All built-in commands. So this should work on pretty much any device I'm not gonna say a hundred percent of the time, but most of the time We're gonna cat the file out and that's just going as we said earlier. It displays. What's inside the file? Control L to clear the screen. I'm gonna pipe that taking the output of that. I'm gonna say while Read line and what this is saying is while we're reading this file For each line and line in this case is a variable. You can you don't have to put line there. You can call it something else But since we're reading the lines, I'm calling it line So for each one of those we're going to do something for each line one at a time So what I'm gonna do here is I'm gonna say put brackets here and I'm gonna do space Dollar sign and parentheses and what this is going to do is whatever we put inside here We're gonna put a command inside here and the output is Going to be replaced that so whatever the output of this command is is actually what's going in this place So I say echo and inside quotations Line that's our variable. So that's our line and we're gonna pipe that into Wc-w Wc is a command that basically it will count the characters Lines and or words within a file in this case. We're not looking at the whole file We're looking at each line individually and this dash w is saying look at the number of words So that should all of this what this should do is output the number of strings or words on this line of that file So now that we have that we're going to say dash EQ Which means equals if that line equals three? Well, then we're gonna do something we're gonna say and and now the two Are they called ampersands I call them n symbols Let me String this down so it's all on one line here make it a little bit easier to read What that does is one of these would just say while this is running do the next command But when you do two of them what that is saying is if this is true Then run the next command otherwise skip this next command and the next command is going to be echo dollar sign line And then semi-colon done to finish our loop here There we go, so let's look at this real quick We're going to cat out the file Then we're going to read it one line at a time and each line is going to be called line then inside these Braces here and brackets here And it is important that you have the correct spacing there should be spaces there work If you don't have those spaces We're going to check the number of words on that line and if that number of words equals three Then we're going to echo out that line. Otherwise skip that line So if we hit enter it it prints out every line that has three words on it from that file We can change that to once again. Let's just let's try six a few lines that have six on it and This works right now. I'm actually inside Z shell is what I'm using but if I went into bash and Ran the same command I get the same output So it works in bash and if I go into basic a basic sh shell and Paste that command by the way I highlight it and then I'm center-clicking on the mouse to paste it in there or you can right-click copy and Right-click paste I'll hit enter and you can see it works there So this should work on stripped-down shells I haven't actually tried it where Ock may be a tool you don't have even though the short the command is shorter but in most cases Pretty much every desktop system Linux system is going to have Ock built-in so this shorter command should work and And like I said, you just change this number to whatever you want it to be and it will output Lines so oh, oh, what's oh that's completely random. That's Ignore that that was not intentional whatsoever You know clear that out. Okay, so anyway Those are two ways of finding all lines with a set number of words on each line from within a text file so I Hope you find that useful because you might need to do that sometime and I hope that you enjoy the tutorial. I hope you continue to enjoy my tutorials I hope you think about subscribing visiting my website filmsbychrist.com. That's Chris of the K as of just a few days ago I completely redid my site. It's much simpler to search through my videos now Still not perfect. It's hard, you know when you guys may be as I do to organize them all but we got playlists and videos and they're all searchable and I Thank you for watching and I hope that you have a great day My website is filmsbychrist.com. That's Chris of the K and there should be a link in the description. Have a great day