Linux cut Command Overview

Updated: Jul 17


Use the cut command to extract columnar data from files by using character or field location


Common Command Options

-c    Character    Extract specified character(s)
-d    Delimiter    Specify the delimiter used to separate data fields
-f    Field        Extract specified field(s)

Examples


For the examples the following file, designated as sample.txt, will be used as input:


This is line 1
This is line 2
This is line 3

Cutting by Field


The data (words) contained in sample.txt are delimited (separated into fields) using the space character. The cut command uses the tab character as the default delimiter. To specify the space character as the delimiter you can use the -d option followed by ' '.


cut -d ' '

Next you choose the field or fields you want to extract. Fields begin with the number 1. To extract the second field from each line of sample.txt use the -f option:


$ cut -d ' ' -f 2 sample.txt

is
is
is

To cut the 2nd and 3rd field from each line use -f 2-3


$ cut -d ' ' -f 2-3 sample.txt

is line
is line
is line

To cut the 2nd field and all fields after it from each line use -f 2-


$ cut -d ' ' -f 2- sample.txt

is line 1
is line 2
is line 3

Cutting by Character


You can also extract specific characters from each line of the input file using in much the same way using the -c option. Here the first 3 characters is extracted from each line:


$ cut -c 1-3 sample.txt

Thi
Thi
Thi

Additional Resources


For more information on the cut command be sure to check out:

73 views

© 2020 by Paul Troncone and Carl Albing