- [Instructor] Hello again, and welcome
to the next video of section four,
cutting a file column-wise with cut.
In the previous video we learned how to search
and mine text inside a file with grab.
In this video, we'll illustrate how to extract the name
of students to another file, or any nth column in the file,
or extract two or more columns.
We may need to cut the text by a column rather than row.
Let's assume that we have a text file
containing students' reports, with columns such
as role, name, mark, and percentage.
Let's see how to do it.
Cut is a small utility that often comes
to our help for cutting in column fashion.
It can also specify the delimiter
that separates each column.
In cut terminology, each column is known as a field.
To extract particular fields or columns,
you use the following syntax.
Field underscore list is a list of columns
that are to be displayed.
The list consists of column numbers delimited
by commas, for example.
Here the second and third columns are displayed.
Cut can also read input text from standard in.
Tab is the default delimiter for fields or columns.
If lines without delimiters are found,
they are also printed.
To avoid printing lines that do not
have delimiter characters, attach the minus s option
along with the cut.
An example of using the cut command
for columns is as follows.
Extract multiple fields as follows.
To print multiple columns, provide a list
of column numbers separated by commas
as arguments two minus f.
We can also complement the extracted fields
by using the minus minus complement option.
Suppose you have many fields, and you want
to print all the columns except the third column.
Then use the following command.
To specify the delimiter characters for fields,
use the minus d option as follows.
The cut command has more options to specify
the character sequences to be displayed as columns.
Let's go through the additional options available with cut.
Specifying the range of characters or bytes as fields.
Suppose that we don't rely on delimiters,
but we need to extract fields in such a way
that we need to define a range of characters
counting from zero as the start of line as a field.
Such extractions are possible with cut.
Let's see what notations are available.
N, from the nth byte character, or field,
to the end of the line.
N dash m, from the nth to the mth
included byte character or field.
M, from the first to the mth
included byte character or field.
We use the preceding notations to specify fields
as a range of bytes or characters
with the following options.
Minus b for bytes, minus c for characters,
minus f for defining fields.
For example.
You can print the first to fifth characters as follows.
The first two characters can be printed as follows.
Replace minus c with minus b to count in bytes.
We can specify the output delimiter
while using the minus c, minus f, and minus b as follows.
When multiple fields are extracted with minus b
or minus c, the output delimiter is a must,
otherwise you cannot distinguish between fields
if it's not provided.
For example.
Awesome, we've successfully learned
about cutting a file column-wise with cut.
In the next video, we'll learn how
to use sed to perform text replacement.