[No audio]
As we are continuing to the text processing
commands, we will 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
texts 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 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 have
a better understanding how the grep or egrep
command works. I have my Linux machine right here
open and again I am logged in as myself, the
machine, the hostname is MyFirstLinuxVM and
which directory I am in, I am in /home/iafzal.
Now one thing I do want to tell you, you're probably
thinking to yourself, hey why Imran alway runs
whoami, and hostname when he sees himself here,
as iafzal, and right here is hostname? The
reason I do that is because a lot of systems, when
you're going to get into the corporate world, a
lot of systems not going to have this type of
prompt, they're probably going to show you either
this symbol or the hashtag. So you're probably not
going to know whether you're logged in as iafzal,
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 have 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, as always check the
version and check for help. So for grep dash dash
version, it will give you the version and now you
could read it, you know where to find the version.
The next one is grep dash dash help. This will
give you all the information about the grep and
its options. If you want to get a quick list of
all the options that are available, you always use
grep dash dash help, if you want to get the longer
description or have 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 this 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
we'll cat it out, and these, this are the contents of
the seinfeld directory. Okay, so now if I just
clear the screen, go back up, and what I will do is I
will grep for Seinfeld, in the filename
seinfeld-characters. It means only give me the
lines there 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 want to grep
for my username iafzal in /etc/password,
I could do the same thing grep iafzal
/etc/password and it will go into that 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, 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
a 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 1000s of words
that matches your criteria, and you want 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-character it gave me 2 Seinfeld.
What if I have, in inside of this while 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. And 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 line and their line numbers. So we used, let's
clear the screen, and if you remember, use -c
option, which gives you the total number of lines
that matched our criteria. Now I wanted to see
those lines to end the line and number where
exactly it happened. So in that case, I hit up 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 want to get everything but that search
keyword. So let's say if I want to get all the
lines that do not match seinfeld. So let's do
grep -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 that 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 use 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 want to combine
the keyword with other texts processors 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 and
seinfeld-characters, right? Right. So what if I
want 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, 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 as
coming up in one line. Let me show you real quick.
You see if I do it this, see went back. So print
dollar one, and curly braces, and quote close. And
now it's giving me only the first column off my
matched criteria. Now, what if I wanted to get only
the first three letters have 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 want the first three that will be one minus
three, one through three because you are picking
the range. And now you will see it is giving you
everything except the keyword line that matches
Seinfeld. Alright, 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 search 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.
I'll go one step back to cd .., it 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, 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 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 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, and let's
cat that seinfeld-characters file, hit enter.
And you'll see these are the contents of that
file. Now what if I want to get the list or lines
that match 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 put double quote,
[No audio]
the keyword and the first keyword I want to search for is
Seinfeld, then the for the second keyword you have
to put a pipe in the middle and then put the
second keyword 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 up 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
a 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
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.
[No audio]