Cutting a file column-wise with cut
We may need to cut the text by a column rather than a row. Let's assume that we have a text file containing student reports with columns, such as Roll
, Name
, Mark
, and Percentage
. We need to extract only the name of the students to another file or any nth column in the file, or extract two or more columns. This recipe will illustrate how to perform this task.
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, use the following syntax:
cut -f FIELD_LIST filename
FIELD_LIST
is a list of columns that are to be displayed. The list consists of column numbers delimited by commas. For example:$ cut -f 2,3 filename
Here, the second and the third columns are displayed.
cut
can also read input text fromstdin
.Tab is the default delimiter for fields or columns. If lines...