Hello, and welcome to the lecture of awk, and this
is one of the commands for text processing
commands. awk is a very powerful command and we
are going to try a few commands that awk has to
offer. So what exactly is an awk? awk is a utility
or language designed for data extraction, and most
of the time it is used to extract fields from a
file or from an input. So let's try a few commands
of our awk. And I have my Linux machine opened
right here on the side, I am logged in as myself,
I logged in as PuTTY. I logged in through the
PuTTY, okay, and the hostname, which is the
server name of my machine is MyFirstLinuxVM.
And I am in my home directory, perfect. Now go ahead
and clear the screen by typing clear. Now let's go
back to the slide, and in this slide, we'll go
over the first command, and the first command is to
check the version of awk. So awk space dash dash
version will give you the version of the
awk utility, when it was written, and who wrote it,
and few description of the awk. Go ahead and
clear the screen, and try the next command. The
next command will actually list the first column
from a file. So let's go to our seinfeld
directory, which is in our home directory, in that
directory, we have a file called seinfeld-characters,
and this is the file we will use for
this lecture. So when you do cat seinfeld-characters,
you're going to see the list of all
the characters or the contents inside this file.
And this file has two columns, as you can see by
the first name and the last name. What if you want
to get only the first name, which is the first
column of this file. Then you will run the
command awk single quote, curly braces, print dollar
one, curly braces, close single quote, close, and the
name of the file, in our case it is seinfeld-characters.
And you're gonna see it only giving us
the first column of this file. If I want
to get the last name, which is the second column
or second field of this file, then I'll do print
dollar two. You could just simply, by the way you
could just simply hit up arrow key and come back
using the left arrow key and change the value. So
in this case, I'm going to change it from one to
two and leave everything as is and do the right
arrow key to go all the way to the enter, hit
enter. And you will see it is giving me the second
column which is the last name of all the
characters inside the seinfeld-characters file.
Then we have the next command is, what if you want
to list the column of an output. So not just, the
actual the contents of a file, but actually the
output of a command. So if I do ls -l in
my directory, which is /home/iafzal/seinfeld
you'll see I have these column which is a
permission, and links, the user, group, size, month,
date, time, and the file name. All are these
different columns are actually fields. If I want
to print the first field, which is the permission
field, and the third field, which is the user
ownership field, then I could simply do ls -l,
awk it, single quote, curly braces, print, dollar
one, comma dollar three, curly braces, close single
quote, close, and you will see you only gonna get
the first and that third column, see how powerful
awk is. Moving on. What if you want to get the last
column of an output. Now if you do ls -ltr,
and what if you wanted to get this last column? You
could count it by saying 1, 2, 3, 4, 5, 6, 7, 8, 9, that it is the
last column. So you hit up arrow key, and you
just put in dollar nine and it will give you the last
column. But there is an easier way instead of you
for counting every column, you could just type NF.
That will automatically gives you the last column
of the output. So there we go, so it's print
dollar nine is the same as print dollar NF, which
gives you the last column.
Then what if you wanted to search for a specific
word from a file using awk command. Okay, so awk
can also do the search for you. So let's say if I
have a file, cat seinfeld-characters, and in that
file, we have all these characters. What if I only
want to search the line that has the word Jerry? I
could use awk, single quote, slash, Jerry, make sure
you specify the exact as uppercase J, slash, curly
braces, print what it found, and, quote close, and
the file name, which is seinfeld, and it is giving
us the Jerry Seinfeld, which is the first line
from our file. As you can see there is no other
line in my content that has Jerry in it. What if I
wanted to do the same thing but instead I want to
do Seinfeld. So I'll do Seinfeld, hit enter,
and now you will see there are two Seinfelds in
there, Jerry Seinfeld, and Morty Seinfeld. So it
matched the exact keywords that we are trying to
search for. Next command I will try is, what if you
wanted to get the list or the fields that is
separated by a delimiter. In this case, we will
use /etc/passwd again as similar to what we
use in the cut command. So if you do cat /etc/passwd,
and you'll see every field in this file
is separated by a colon. So you could also use awk
command here instead of cut command to get the
fields. So if you want to get the first field,
which tells you the user name only, then you
could use awk dash minus F delimiter, and in this
case, the delimiter is colon, and then single
quote, curly braces, dollar one, curly braces
closed, single quote closed, and /etc/passwd.
And you will see you got all of the user names
from that file. Same thing, what if you want to
get the home directory. So home directory, we know
that it is actually the sixth field. So you type
six, and you'll get all the home directory. So cut
and awk and this regard works similarly. Next one
we'll learn is, what if you wanted to replace a
column that matches a certain keyword. So let's
say if you do echo Hello Tom, and in this case,
the dollar two is Tom, so it will replace dollar
two with Adam, and I'll print it. So let's try it.
So when you do echo, let's say Hello Tom, and you
hit enter, it will echo it out, and it will echo it
back to you right. So if you run this command
again by hitting up arrow key, and you do awk,
single quote, curly braces, dollar two, and in this
case dollar two we all know is Tom, and I want to
replace Tom let's say with Adam, and we type Adam here,
close, and then we do colon, no, a semicolon,
sorry, and then print dollar zero, print dollar
zero, dollar zero meaning whatever it finds
print it now. So we will do curly braces
close, and quote close, single quote close, and hit
enter and you will see now instead of doing Hello
Tom, it is doing Hello Adam, because we replaced the
second column with Adam. Just like that we could
use, we could also use our seinfeld-characters
file. So let's do cat on seinfeld-characters file
right. Now what if I wanted to replace every last
name with my name. So my name is Imran right, as
you all know. So I want my name to be as a last
name for every second column. So in this case, what I
will do is, cat seinfeld, and I will do awk, same
thing, dollar two which is the second column
equals double quote Imran, double quote close,
semicolon, print dollar zero, curly braces close,
single quote close, hit enter, and you will see now
every last name is my name. That's how you could
change, that's how this awk command become so
powerful to make changes to your file. Moving on,
what if you want it to replace the second, oh, I
already did that, perfect. So I'm one step ahead.
This is something that I just showed you. Then
next one is, okay, what if you want to get the
lines that have more than 15 byte size. Okay, so
if you do cat seinfeld, you probably notice there
are some names that are smaller and there are some
names that are far bigger, like this George
Steinbrenner, and Estelle Costanza. So these are
the bigger names and it has a bigger bytes. So
what if I only wanted to get the names of those
lines or the lines that has those names ,
that has more than 15 bytes or length 15
characters. So I will do, first of all, I'll do
cat seinfeld, and then I'll do awk, or what I could
do instead of cating it, I'll just start with
awk, and I'll put the file name at the end, then
single quote, length,
[No audio]
parentheses, dollar zero,
parentheses closed, greater than 15, single
quote close, and the name of the file, in this case
seinfeld-character, and you will see it is giving
me those two lines that has the size, the length
that is more than 15. If you do 14, it will find a
few more. It also found, this time it also found
George Costanza. So that's how you could use this
awk command in terms of the length. Then moving
on, get the field of matching seinfeld in slash
home slash iafzal. So when I do, when I go to my
home directory, just type cd, hit pwd, and you will
see you are in your home directory. In my home
directory, when I do ls -l, I have all these
files and directories. Now what if I wanted to get
the list that only matches seinfeld. I'll do ls
-l, awk, single quote, curly braces, if,
parentheses, dollar nine, and we all know dollar
nine is the last column, you could do dollar nine
or you could do NF, equal equal, double quote,
[No audio]
seinfeld. If it matches seinfeld, then you have to
print it, so print, dollar zero, semicolon, and then
you have to close it with curly braces, and then a
single quote, and hit enter. And you will see, oops,
I got some error message. Let's see, where's that
ls -l, awk, if dollar nine equals seinfeld,
oh, I didn't put the parentheses right here. So I
hit up arrow key, I have to put the parentheses to
close my argument, and now hit enter, and it will
give me only the file or a directory that matched
the last field or the ninth field as equal to
seinfeld. Then the last one I'll give you an
example of the awk command is, what if you want to
find the total numbers of fields in a file or in
an output. So let's say if you run ls -l here,
and you count it to find out which column that you
want it to get, right. So let's say if you were
looking for the last column, and you want to know
how many columns this output has, you could
count 1, 2, 3, 4, 5, 6, 7, 8, 9, or you could just simply run the
command ls -l, and awk it, print it, NF, no
no dollar NF, just NF, curly braces close, and
single quote close, hit enter, and it will tell you
it has nine fields. The top one, that you see 2,
it is actually when you do ls -l, you're gonna
see right here on top, it says total 280. So you
could just ignore that, it's basically giving you
there are two fields, and the rest every line has
nine fields. So this is all about awk. There are
just, there are, there's so much that you could do
with the awk command that I probably I don't have
time to cover, but you could go ahead and try and do
always man on awk to find out what are the other
options that you have. There are also gawk, pawk,
gpawk, dgawk, and there's different ones that you
could use. And there are different options that
you could use. Try every one of them if you have
time, so you have a better grip on the command awk.
[No audio]