As we are continuing to the text processing commands, we will we'll go over the grep and Ekrem commands,
that is also as one of the powerful commands in terms of text processing.
So what exactly is a grep?
The GRUP command, which stands for global regular expression, print processes, text line by line
and print any lines which match a specified pattern.
So basically in simple words, it's a search technology or a search feature Lennix that allows you to
search for a specific feature or Pacific keyword, actually.
So not feature a specific keyword that you look for in a file or an output from a command.
So let's get into a Linux machine and I will show you a few of the commands and examples.
So this way you have a better understanding how the grep or Ekrem command works.
I have my Linux machine right here opened and again I am logged in as myself.
The machine, the hostname is my first Linux VM and which directory I am and I am in flash home.
I have sold.
Now one thing I want to tell you, probably thinking to yourself, Hey, why Ron always run, who am
I?
And Hostname when he sees himself here as I have Sol and right here is a hostname.
The reason I do that, because a lot of systems when you going to get into the corporate world, a lot
of systems not going to have this type of prompt, they are probably going to show you either this symbol
or the hash tag.
So you're probably not going to know whether your login as I absol or you're going to logged into the
right machine.
But if you do see it perfect, that's great.
But if you don't, that's why I want you to get used to of these few commands, who am I hostname and
which directory you are in before you start making any changes to your system.
Anyway, let's clear the screen and the first command of you will run for Crêpe.
As always, check the version and check for help so far.
Crêpe dash dash version.
It will give you the version I now you you could read it.
You know where to find the version.
The next one is grippe.
Dash dash help.
This will give you all the information about the grep and its options.
If you wanted to get a quick list of all the options that are available, you always use grep, dash,
dash, help.
If you wanted to get the longer description or of every option you could use.
Man and do crêpe.
And this will give you a detailed information about the grep come in anyway.
So we know the version.
We know how to get help.
Now let's get to the second lab question, which is to search for a keyword in a file.
So I am in my home directory.
I'll go back to the Sinophile directory in there.
I have a file called Seinfeld Characters.
I'll carry it out.
And these this are the contents of the Seinfeld directory.
OK, so now if I just clear the screen, go back up and I will do is I will grep for.
Sign filled in the file name sign signed filled characters.
It means only give me the lines that matches the word Seinfeld when I hit enter.
There are two lines that match the word Seinfeld, which is Jerry Seinfeld and more Seinfeld.
I could see the same way I could do with other files as well.
Let's say if I wanted to grab for my username I have and etsi password, I could do the same thing.
Grupp I have also I Schatz's like password and it will go into that pot and search for that specific
keyword.
And it brings back one line because there is only one user in ETSI password that has the word I have
salt and that's me of course.
All right.
Next command are the next Syntex that will learn as search search for a keyword and count it.
So if I am doing, let's hit up Parakey twice and this is a command we ran again, which is to grab
Seidenfeld from simple characters and it gives me two.
But what if you have a file that has thousands of words, that matches your criteria and you wanted
to know how many times it shows up, then you could just simply do grep minus C and it would tell you
that it is showing up two times.
So that's what minus C is used for.
The next one is minus I, which is the one that you should always use and I have a habit of using.
Grew up with all those minus.
What minus I does is actually search for keyword and ignores the key sensitive meaning.
If I have grep Seinfeld of character it gave me to sign.
But what if I have an inside of this file.
I have a Seinfeld with lowercase.
So let's say if I'm groping for Seinfeld a lowercase s then enter.
It's not giving me anything, even though I know there was a word Seinfeld in it.
So that's why I always use minus.
I option to tell my system, hey, ignore any upper case or lower case from that keyword and from that
file and you do months I it actually grips and gets you the result regardless of whether they are written
in upper case or lower case.
Next one is nd displays the match lines and the line numbers.
So we used and it's clear the screen and if you remember you use minus C option, which gives you the
total number of lines that matched our criteria.
Now I wanted to see those lines to end in line and no where exactly it happened.
So in that case I hit up at Parakey and replace minus seven months and hit enter and you will see it
will give me the two lines that match my criteria.
And it also telling me the first line is actually on the line, number one, and the second match line
is line number eight.
Isn't that awesome?
Yeah, I like it.
OK, next one is minus V also very powerful.
Meaning what if you want to get everything but that search keyword.
So let's say if I wanted to get all the lines that do not match Seinfeld Celesta grep minus V.
Seinfeld, or I could just do V.I. or i.p to include I and to ignore sign.
Filled from Seinfeld characters, and now you will see I have a list of every line that the whole five
content, but this does not have that to Jerry Seinfeld and Morty Seinfeld.
This these two lines are omitted or are excluded from it because I use the minus V option.
So minus V is basically telling the system, give me everything except this search keyword.
All right.
The next one is what if you want to combine the key word with other text processes, commands that we
have learned previously, right?
Yes, we could definitely do that.
So let's say if I type grep Seinfeld or let me run the same command, the last comment grep minus V,
Seinfeld and Seinfeld characters.
Right?
Uh, right.
So what if I wanted to print from this output?
I only wanted to print the first column.
Then you could.
What you could do is you could hit up a rocky pipe.
It right here.
Orckit and Dahler, by the way, chop down the line.
It is not actually putting into a new line.
It's a continuation of the same line.
If you make your window bigger, you will see it is coming up in one line.
Let me show you real quick.
You see, if I do it, this segment went back.
So print $ one.
And.
Curly braces and cold clothes, and now it's giving me only the first column off my mashed criteria.
Now what if I wanted to get only the first three letters of this output?
Can you guess what could that be?
I'm sure you know how to do it, but if you forgot, that's fine.
You hit up Parakey and now you could put another pipe.
And now that you could cut minus C four characters and you win the first three.
That would be one minus three, one through three because you picking the range and now you'll see it
is giving you everything except the key word line that matches Seinfeld.
All right, let's clear the screen.
Let me make my window a little smaller and I'll move on to the next one, which is as minus L.
So Grupp does not only search for a keyword from a file, it actually search for any keyword from any
output of a command.
So let's see if I have let me go to my home directory.
So it's home.
I have all signed.
Plus I'll go one step back to Dot will bring me back to my home level directory.
Now in here when I do X minus L, you're going to see the listing of all the files and directories.
Now what in this what if and this output, I only wanted to see the line that matched the file or directory
desktop.
So I hit up arrow key, pipe it and now I'll grab it for desktop.
And let's say just for giggles, I'll use the lowercase D and hit enter and it's not going to give me
anything.
Why?
Because if you notice, the desktop has uppercase D, so you could either do hit up key and replace
the upper lowercase T with a case or leave it as it and put minus high option to ignore the case since
it hit enter and you're going to see that it is pulling only that line that matched the word desktop.
Isn't that cool?
All right.
Now let me go into the grep and ekra functionality.
Now, you know, grep works perfectly on every keyword.
Now the ekra comes into play when you want to search two words from a file.
So let's clear the screen.
Let's go into the Seinfeld directory.
Inside file directory, we have a file called Cipha.
Let's get that Seinfeld characters file hit Enter and you'll see this.
These are the contents of that.
Well, now, what if I wanted to get the list or lines that match not only Seinfeld, but also Castanza?
For that, we will use the E, which is a little powerful comen then grep.
So be E grep minus I.
Of course I want to ignore upper case or lower case.
Here you put double code.
The key word and the first key word I to search for Seidenfeld.
Then for the second key word, you have to put a pipe in the middle and then put the second key word
would it tends to close with the double quotes and of course, specify the one, the frame from where
you are trying to search, you hit enter and now you will see it is picking up.
We make this big or so and hit the Parakey.
So you know what exactly the output.
Now you see it's giving me Jerry Seinfeld, George Costanza, Frank Constanza, Estelle Costanza and
Morty signed.
So all the lines that match Seinfeld and Costanza's are there.
So again, if you wanted to talk it out for second column, if you wanted to cut it out for certain
characters, you could definitely do that by putting pipe to one another.
So this is this lecture is all about Grupp and Ekra.
And again, I want you to try man on GRAP and try different options that are available to you.
Like these are the different options.
Try it if you have time.
Otherwise, the ones I showed you, these are more than enough for you to perform your everyday daily
task while you are at the job.