Linux provides the cut command which can be used to cut specified parts of the file via the command line or bash. The cut command operates over the provided file and the result is printed into the standard output. The cut operation can be done with different specifiers like byte, character, and field.
cut Command Syntax
The Linux cut command has the following syntax which is the same as most Linux commands.
cut OPTION FILE
- OPTION is the option part where we can specify the cutting range. This is required.
- FILE is the file we want to cut. This is required.
The cut command prints newly curated content into the standard output. If you want to save the newly curated content you can redirect the output into a file.
cut Command Help
The help information and options about the cut command can be displayed by using the man page of the cut command like below.
man cut
If the man is not installed or alternative way is using the –help option which prints options and descriptions in short.
cut --help
Usage: cut OPTION… [FILE]… Print selected parts of lines from each FILE to standard output. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -b, --bytes=LIST select only these bytes -c, --characters=LIST select only these characters -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter -f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified -n (ignored) --complement complement the set of selected bytes, characters or fields -s, --only-delimited do not print lines not containing delimiters --output-delimiter=STRING use STRING as the output delimiter the default is to use the input delimiter -z, --zero-terminated line delimiter is NUL, not newline --help display this help and exit --version output version information and exit Use one, and only one of -b, -c or -f. Each LIST is made up of one range, or many ranges separated by commas. Selected input is written in the same order that it is read, and is written exactly once. Each range is one of: N N'th byte, character or field, counted from 1 N- from N'th byte, character or field, to end of line N-M from N'th to M'th (included) byte, character or field -M from first to M'th (included) byte, character or field
Example Text File
During the cut command examples, we will use the following text file. This text file contains the country name and some cities of the country.
USA Newyork Florida UK London Newcastle Turkey Istanbul Ankara
Cut Specified Character Range
The cut command can be used to cut according to the character index. We can specify the character index or multiple characters range to return. In the following example, we will cut the first character.
cut -c 1 cities.txt
U U T
Alternatively, we can specify multiple characters by providing every character value index number. The index numbers are delimited with a comma. In the following example, we will cut and return the first 3 characters from the cities.txt file with the 1,2,3 byte specifiers.
cut -c 1,2,3 cities.txt
USA UK Tur
Cut Between Specified Character Index
Another way to cut specified bytes is by using the character range. The dash is used to specify the start and end index of the characters we want to cut and return. In the following example, we will cut and return characters from 1 to 3 by using the 1-3 parameter.
cut -c 1-3 cities.txt
USA UK Tur
Cut for Multiple Character Index Range (Fields)
Multiple character ranges can be specified by delimiting them with commas. In the following example, we will cut the 1-3 and 6-9 character ranges.
cut -c 1-3,6-9 cities.txt
USAewyo UK ndon Tury Is
We can specify the start of the character to the end of the line. In the following example, we will cut from character index 3 to the end of the line.
cut -c 3- cities.txt
A Newyork Florida London Newcastle rkey Istanbul Ankara
In reverse to the previous example, we can cut from the start of the line to the specified character range. In the following example, we will return from begging of the line to the 5th character.
cut -c -5 cities.txt
USA N UK Lo Turke
Cut Specified Byte Range
The cut command can be used to cut in byte by byte mode. We can specify the byte range we want to cut from the specified file and print the result into the standard output. The byte can be specified with the -b option like below. We will also provide the byte index number where the index starts from 1. In the following example, we will cut and return the first character of every line.
cut -b 1 cities.txt
U U T
Alternatively, we can specify multiple bytes by providing every byte value index number. The index numbers are delimited with a comma. In the following example, we will cut and return the first 3 bytes from the cities.txt file with the 1,2,3 byte specifiers.
cut -b 1,2,3 cities.txt
USA UK Tur
Another way to cut specified bytes is by using the byte range. The dash is used to specify the start and end index of the bytes we want to cut and return. In the following example, we will cut and return bytes from 1 to 3 by using the 1-3 parameter.
cut -b 1-3 cities.txt
USA UK Tur
Multiple byte ranges can be specified by delimiting them with commas. In the following example, we will cut the 1-3 and 6-9 byte ranges.
cut -b 1-3,6-9 cities.txt
USAewyo UK ndon Tury Is
Cut From Specified Index To The End Of Line
We can specify the start of the byte to the end of the line. In the following example, we will cut from byte index 3 to the end of the line.
cut -b 3- cities.txt
A Newyork Florida London Newcastle rkey Istanbul Ankara
In reverse, to the previous example, we can cut from the start of the line to the specified byte range. In the following example, we will return from begging of the line to the 5th byte.
cut -b -5 cities.txt
USA N UK Lo Turke
Cut Specified Columns/Fields
The cut command can be used to cut according to the fields or columns. A delimiter is specified in order to slice every line and the provided column or field number is returned. The delimiter is specified with the -d option. The field number is specified with the -f option. In the following example, we will use a single space as a delimiter and return 1st column or field.
cut -f 1 -d " " cities.txt
USA UK Turkey
We can also use different delimiters like regular characters which can be a single character or multiple characters. Also, the popular CSV file format uses commas as delimiters where we can set commas as the delimiter to cut specific columns or fields. In the following example, we will use the comma as the delimiter.
cut -f 2 -d "," cities.txt
Specify Output Delimiter
Output delimiter can be specified for the cut command. The cutting portion may consist of multiple fields or columns. As multiple fields or columns can be selected in the output a specified delimiter can be used. In the following example, we will select multiple columns from 1 to 3 and use ; as the delimiter.
cut -f 1-3 -d " " --output-delimiter ";" cities.txt
USA;Newyork;Florida UK;London;Newcastle Turkey;Istanbul;Ankara
From the output, we can see that spaces are changed with the ; .
Save Output Into A File
By default, the cut command output is printed into the standard output. If we need to put the result into a file we can redirect the output of the cut command into a file by using the redirection operator for the bash. In the following example, we redirect the cut command output into a file named newcities.txt.
cut -f 2 -d " " cities.txt > newcities.txt
cut Command Version
Even if it is not a popular command which is not updated regularly we may need to get the cut command version with the –version option like below.
cut --version
cut (GNU coreutils) 8.32 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later https://gnu.org/licenses/gpl.html. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by David M. Ihnat, David MacKenzie, and Jim Meyering.
As we can see that the current version of the cut command is 3.82.