Instructor: As we are continuing
to the text processing commands,
we'll go over the grep and egrep commands
that is also as one of the powerful commands
in terms of text processing.
So what exactly is a grep?
The grep command, which stands
for global regular expression print,
processes text line-by-line and prints any lines
which match a specified pattern.
So basically, in simple words, it's a search technology
or a search feature in Linux that allows you to search
for a specific feature or a specific keyword, actually,
sorry, 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'll have a better understanding how the grep
or egrep command works.
I have my Linux machine right here opened and again,
where I am logged in as myself,
the machine, the host name, is MyFirstLinuxVM.
And which directory I am in, I am in /home/iafzal.
Now, one thing I do wanna tell you,
probably thinking to yourself, hey,
why Imran always run whoami and hostname
when he sees himself here as iafzal
and right here is the hostname?
The reason I do that is because a lot of systems,
when you're gonna get into the corporate world,
a lot of systems not gonna have this type of prompt.
They're probably gonna show you either this symbol
or the hashtag.
So you're probably not gonna know whether you're logged in
as iafzal or you're gonna log 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 these few commands, whoami, 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 we will run for grep
is always check the version and check for help.
So for grep --version, it will give you the version,
I know you could read it,
you know where to find the version.
The next one is grep --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 --help.
If you wanted to get the longer description
of every option, you could use man and do grep,
and this will give you a detailed information
about the grep command.
Anyway, so we know the version, we know how to get help,
and 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 seinfeld directory.
In there, I have a file called seinfeld-characters,
I'll cat it out, and these are the contents
of the seinfeld directory.
Okay, so now if I just clear the screen, go back up,
and I will do is I will grep for Seinfeld
in the file named seinfeld-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 Morty Seinfeld.
I could, same way I could do with other files as well,
let's say if I wanted to grep for my username, iafzal,
in /etc/passwd, I could do the same thing,
grep iafzal /etc/passwd, and it will go into the file
and search for that specific keyword.
And it brings back one line because there is only one user
in /etc/passwd that has the word iafzal
and that's me, of course.
All right, next command or the next syntax that we'll learn
is search, search for a keyword and count it.
So if I am doing, let's hit up arrow key twice,
and this is the command we ran again,
which is to grep Seinfeld from seinfeld-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 -c,
and it would tell you that it is showing up two times.
So that's what -c is used for.
The next one is -i, which is the one
that you should always use, and I have a habit
of using grep with always -i.
What -i does is it actually search for keyword
and ignores the case-sensitive,
meaning if I have grep Seinfeld seinfeld-characters,
it gave me two Seinfeld, what if I have,
inside of this file, I have a seinfeld with lowercase s?
So let's say if I'm grepping for Seinfeld with lowercase s
then hit Enter.
It's not giving me anything, even though I know
there is a word Seinfeld in it.
So that's why I always use -i option to tell my system, hey,
ignore any uppercase or lowercase from that keyword
and from that file.
When you do -i, it actually greps
and gets you the result regardless whether they are written
in uppercase or lowercase.
Next one is n, displays the matched lines
and the line numbers.
So we used, let's clear the screen, and if you remember,
we used -c option, which gives you the total number
of lines that matched our criteria.
Now, I wanted to see those lines too and the line
and number where exactly it happened.
So in that case, I hit up arrow key and replace -c with -n.
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 matched line is line number eight.
Isn't that awesome? Yeah, I like it.
Okay, next one is -v, also very powerful,
meaning what if you wanted to get everything
but that search keyword?
So let's say if I wanted to get all the lines
that do not match seinfeld, so let's do grip -v seinfeld,
or I could just do -vi or -iv to include i as in to ignore,
seinfeld from seinfeld-characters.
And now you will see I have a list of every line,
the whole file content, but this does not have that two,
Jerry Seinfeld and Morty Seinfeld,
these two lines are omitted or are excluded from it
because I used the -v option.
So -v is basically telling the system,
give me everything except this search keyword.
All right, the next one is what if you wanted
to combine the keyword 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 command,
grep -vi seinfeld in seinfeld-characters, right?
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 arrow key, pipe it right here,
awk it and dollar, by the way, it chopped 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 was coming up in one line.
Let me show you real quick.
You see, if I do this, see, it went back.
So print $1
and curly braces and code close,
and now it's giving me only the first column
of my matched 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 arrow key,
now you could put another pipe,
and now that you could do cut -c for characters
and you wanted the first three,
that would be one minus three, one through three,
because you're picking the range.
And now you'll see it is giving you everything
except the keyword 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 ls -l.
So grep does not only search for a keyword from a file,
it actually searches for any keyword
from any output of a command.
So let's say if I have, let me go to my home directory,
So it's /home/iafzal/seinfeld, so I'll go one step back,
so cd .. will bring me back to my /home/iafzal directory.
Now, in here, when I do ls -l, you're gonna see the listing
of all the files and directories.
Now, what, in this, what if in 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 grep it for Desktop.
And let's say just for giggles,
I'll use the lowercase d and hit Enter,
and it's not gonna give me anything, why?
Because if you notice, the Desktop has uppercase D.
So you could either do hit up arrow key
and replace the lowercase d with uppercase
or leave it as it and put -i option
to ignore the case sensitive, hit Enter,
and you're gonna 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 egrep functionality.
Now, you know grep, grep works perfectly on every keyword.
Now, the egrep 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.
In seinfeld directory, we have a file called seinfeld,
let's cat that seinfeld-characters file, hit Enter,
and you'll see this, these are the contents of that file.
Now, what if I wanted to get the list
or lines that matched not only Seinfeld but also Costanza?
For that, we will use the egrep,
which is a little powerful command than grep.
So it will be egrep -i, of course,
I want to ignore uppercase or lowercase.
Here, you'll put double quote.
The keyword, and the first keyword I wanted
to search for, Seinfeld.
Then for the second keyword, you have to put a pipe
in the middle and then put the second keyword,
which would be Costanza.
Close with the double quotes and, of course,
specify the file name from where you are trying to search.
You hit Enter and now you will see it is picking up,
let me make this bigger, so, and hit the upper arrow key
so you know what exactly the output.
Now you see it's giving me Jerry Seinfeld,
George Costanza, Frank Costanza, Estelle Costanza,
and Morty Seinfeld.
So all the lines that match Seinfeld
and Costanzas are there.
So again, if you wanted to awk it out for second column,
if you wanted to cut it out for certain characters,
you could definitely do that
by putting pipe after one another.
So this is, this lecture is all about grep and egrep
and again, I want you to try man on grep
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.