Hello everyone and welcome to the lecture of cut
command, which is part of the text processor
commands. What is a cut? A cut command is a
command line utility that allows you to cut parts
of lines from specified files or piped data, and
print the result to standard output. It can be
used to cut parts of a line by delimiter, by
position, and characters. And the commands that we
are going to learn are as follows. So there are,
the first command that you can run is cut
filename, again, do not run cut with just a file
name because it's not going to work, you have to
specify the option. So I will log into my Linux
machine and this time, I'm going to log in
through my PuTTY session. Right here, I have my
Linux machine. And if you want to log into the
PuTTY session, you have to log into your console,
run ifconfig command, get your IP address, which
is 192.168.1.162, and put into the PuTTY, and then you
can log into through the PuTTY. But if you do not
have this IP address, then you go to the Oracle
VirtualBox network setting and you change the
network setting to bridged adapter and then it
will allows you to get this IP address. Anyway, so
now the first command we'll use is cut and the
file name, and which file name are we going
to use. I have logged in as myself and I am in my
Linux machine and which directory I'm in, I'm in
home directory. In my home directory, I
have a directory called seinfeld. I'll go into the
seinfeld directory. Inside of that directory, I
have a file named seinfeld-characters. And this is
the file that I am going to use for this cut
lecture. When you do cat on seinfeld-characters,
you are going to see the list of all the lines
inside of the seinfeld-characters. And these are
the actual names of all the characters inside of
the Seinfeld show. I don't want you to focus on
the Seinfeld show itself, but I just want you to
focus on the content of this file. If you do not
have this seinfeld-characters file, please create
it right now. Pause this video and you should,
the file should have this content inside of your
file seinfeld-characters. Anyway, so let's assume
that you do have this file and you do have exactly
all that content inside of this file. Now the
first thing is you do cut seinfeld, and as I said,
you cannot run cut without any options, it will
give you the error message. Then the next command
that you should run is the cut --version. So
cut --version. And it will give you the
version, right now I'm running the version 8.22,
your version is probably new or the older it does
not matter which version you have, the cut
functionality is the same. Also, you could do cut
--help to find information about each
option that it has to offer. You could also do man
cut to find detailed information about the command
or utility cut. And you could go down by pressing
the spacebar and hit Q to get out of it. The next
command I want to show you is, if you want to get
the first character or the first letter of the
file seinfeld-characters. So let's see if you have
seinfeld-characters file, let's cat it out. And
you're gonna see that if you want to only get this
first letter of every line, then you will run the
command cut -c1 and the file name which is
seinfeld-characters. And it will give you the list
of all the first letters in every line. Then the
next command we will run is, what if you want to
pick and choose the characters. So let's do cat
seinfeld again. And let's say if you want the
first letter, then the third letter, and then the
fifth letter, and then the eighth letter. Then you
could do that by simply doing cut -c1 or
three or five or seven, whichever characters that
you want to pick and choose, and specify the file
name. And in this case, it actually has pulled all
those letters and jotted down together as one
word. Then next one is a list, a range of characters.
So what if you want to list the range of character.
Meaning, let's do cat seinfeld again, and you want
to list the first character all the way through
the fifth character. Then you could do by simply
doing cut -c sorry, -c1 through
five or through three, whichever the range that
you want to specify. You hit enter and you will
see you will get the output exactly that will show
only five letters. The next one is list specific
range of characters. What if you want to, let's do
cat seinfled, what if you want to do right here, one to three,
and then six to eight, so from here to here. So
it is as easy as we have done earlier. So cut -c1-3,
then you put ,6-8, or 6-9,
depending on your requirement, you
hit enter, and you will see that it is giving you
the first three letters, and then the six to eight letters.
[No audio]
The next example that we have is we could also get
the list or output by the byte size. So if you do
cat seinfeld, all of these letters are actually
equivalent to the bytes as well. So byte pretty
much works as the characters as well, or the
letters. So if you do cut -b1-3,
seinfeld, you'll see its output is
pretty much similar or exactly, I would say, like
the -c1-3. Anyway, the byte
might change on a certain spaces. So just be
careful what you want to use. Then the next one is,
what if you wanted to get a list of columns of a
file by specifying the delimiter. So what is the
delimiter. So let's take an example of /etc/passwd file.
So I want you to do cat /etc/passwd
file, hit enter, and in this file, you
will notice, this is the file by the way is
used when you create a user in Linux, this file
is the one that manages all the users or keep
record of all the users. So that is something we
will learn later, anyway, so in this file, if you
notice, there are columns, and these columns are
divided by this little colons. So you see this x
and right after x, it has this colon, and then this
column, colon, and column. So let's say I'm not
going to use column, I will use field so it will be
easier to understand. So colon, and field, colon,
and field. So what if you wanted to get a field by
specifying the delimiter and in this, in this
case, the delimiter for you would be colon. So, if
you want to do cut -d, and you specify
delimiter, with colon, -f, and you specify
the field six, then the name of the file which is
seinfeld, you will see, oh, sorry, that's not seinfeld,
/etc/passwd, /etc/passwd, then you will see
it is giving you the sixth feild that is
right after the colon. Let's do cat /etc/passwd again,
then I could explain you, see, the
first is this, second is this, third, fourth,
fifth, sixth, and this is what it's grabbing for
you. So in my case, if I have my user account,
iafzal, this is my first field, second, third,
fourth, fifth, and sixth. So this is my home
directory. So six column is actually my home
directory, so it is pulling that home directory.
You could also use the delimiter on many different
symbols as well, it's not only colon. If this file
has a field that is separated by, let's say a
comma, then you could use just like this comma, or
if it was separated by let's say, question mark,
then you will put question mark. Anyway, so with a
colon, let's say if you want to pull the sixth and
seventh field, then you can just specify six to
seven, and hit enter, it will give you the two
fields. Next one is, again, I already covered that,
okay, and then the last one I have is the cut file
not only works on the file itself, it also works
on an output of a command. So let's say if you
are in your home directory, /home/iafzal, and
you do ls -l, you will see the list of all the
files and directories. What if you want to get only
these three bits. These three bits
will, actually are the permissions of the file
which is owned by the user. So if you want to get
only that, then you will do ls -l, then cut
-c2-4, right, so it will be
two, three, and four. Hit enter and you will see it
is only giving you that out. So that's how the cut
command, and cut utility works. I have given you many
examples, you should try it, and try it in a different way
through it to a different files, or to different
output, and it will be a lot of fun to work with it.
If you have any questions please do send me a
message, and I hope this lecture works well in the cut command.
[No audio]