Introduction
The cut
command is a command-line utility that extracts specific sections of a specified file or piped data and prints the result to standard output. The command cuts parts of a line based on fields, delimiters, byte positions, and character positions.
In this tutorial, you will learn what the cut
command is and how to use it.
Prerequisites
- A system running Linux.
- Access to the terminal.
cut Command Syntax
The cut
command has a simple syntax:
cut [option] [file]
Specifying an [option]
is necessary. Otherwise, the command outputs an error. The [file]
argument determines the name of the file you want to process.
If you don't provide a filename, the cut
command reads data from the standard input (e.g., input from another command in a pipeline) and processes it accordingly. If you specify multiple filenames, the cut
command concatenates the requested content.
Note: Another Linux command that produces formatted outputs is the awk command.
cut Command Options
The cut
command options specify how to extract data from input by defining the delimiter and determining whether to cut by byte position, field, or character.
The available options are:
Option | Description |
---|---|
-f (--fields=LIST) | Specifies which fields to extract from the input based on a delimiter. |
-b (--bytes=LIST) | Instructs which bytes are to be extracted from each input line. |
-c (--characters=LIST) | Specifies which characters are to be extracted from each input line. |
-d (--delimiter) | Specifies a delimiter to use instead of the default tab delimiter. |
--complement | Instructs cut to display all the bytes, characters, or fields except the selected ones. |
-s (--only-delimited) | Instructs cut not to print the lines that don't contain delimiters. The default setting is to print the lines that don't contain delimiter characters. |
--output-delimiter | Allows you to specify a different output delimiter. By default, cut uses the input delimiter as the output delimiter. |
The -f
, -b
, and -c
options use the LIST
argument to select specific fields, bytes, or characters. Provide a single number (N
), a list of numbers separated by commas or ranges of numbers.
A range like N-
means "from N
to the end of the line," while N-M
means "from N
to M
." A range like -M
selects from the start to M
.
Linux cut Command Examples
The cut
command has many uses for processing and formatting data.
To understand how the cut
command works, use the echo command to create a file (example.csv) and redirect the output to it. For example:
echo -e "name,age,city\nAlice,30,New York\nBob,25,Los Angeles\nCharlie,35,Chicago" > example.csv
The command has no output but creates a file example.csv with the following content:
name,age,city
Alice,30,New York
Bob,25,Los Angeles
Charlie,35,Chicago
Confirm the file contents with the cat command:
cat example.csv
Below are the most common cut
command use case examples.
Note: cut
only works on a copy of the content and does not modify the original file. The command extracts and displays data but leaves the original file unchanged.
Linux cut with Field Selection
The cut
command in Linux allows users to extract specific fields from text files or input based on a defined delimiter. The following text explains field selection based on characters, bytes, and field numbers.
Field Selection Based on Character
To cut by characters, specify the -c
option. The syntax is:
cut -c [LIST] [file]
The [LIST]
argument specifies the characters to be extracted from each line of [file]
.
For example, if you want to extract the first and fifth characters from each line of the example.csv file, run:
cut -c1,5 example.csv
The command extracts the specified characters from each line of the example.csv file and displays them as output.
The cut
command reads data from standard input (stdin) if you don't specify a file. This allows you to use cut
in combination with other commands in a pipeline.
For example, use cut with the who command:
who | cut -c 1-4
In the example above, cut
extracts characters one through four from each input line.
Additionally, use cut
to extract multiple different characters from a line. For example, display the username and login time of all logged-in users:
who | cut -c 1-4,18-
The command extracts the first four characters of each username and the character positions starting from 18 to the end of the line, allowing you to view usernames and their login times.
Field Selection Based on Byte Offsets
The -b
option allows you to extract data using bytes. The syntax is:
cut -b [LIST] [file]
The [LIST]
argument are the bytes to extract from each line of [file]
.
Depending on what you want to extract, you can cut a single byte, multiple bytes, or a byte range. To cut from a specific file, specify the filename at the end of the command.
For example, to extract the first byte from each input line of the example.csv file, run:
cut -b 1 example.csv
Field Selection Based on Field Numbers
The cut
command allows users to select specific fields from lines of text, making it useful for processing structured data like CSV (comma-separated values). When using field selection based on field numbers, you can extract complete fields rather than individual characters.
To select fields, use the -f
option along with the -d
option to specify the delimiter that separates the fields.
For example, extract just the second column, which represents the age. To do that, use the cut
command with the -d
option to specify the delimiter (a comma in this case) and -f2
to select the second field:
cut -d',' -f2 example.csv
This extracts the second field from each line using the comma (,
) as the delimiter.
Linux cut Delimiter Options
Delimiters are characters that separate fields in structured data, such as commas in CSV files or spaces in log files. By using delimiter options with the cut
command, you customize the extraction of specific fields from complex datasets.
Default Delimiter
The default delimiter for the cut
command is the tab character. This means that if you do not specify a delimiter using the -d
option, cut
assumes the fields in your input are separated by tabs.
If your data uses a different delimiter, such as commas, spaces, or colons, you must specify the delimiter with -d
.
Custom Delimiter
If the default tab character doesn't separate the fields, use the -d
option to specify a different delimiter. That means the character specified after the -d
option is considered the separator in the lines. The syntax is:
cut -d [delimiter] [file]
In place of the [delimiter]
argument, specify the delimiter you want. You can use any character as a delimiter.
In the following example, we use whitespace as a delimiter and print the second field:
echo "phoenixNAP is a global IT services provider" | cut -d ' ' -f 2
Linux cut Command: Additional Options
The cut
command offers several additional options that enhance its functionality for extracting and manipulating text.
For example, the --complement
option prints everything except for the character/byte/field at the specified position. The following command prints all fields except the first:
cut example.csv -f 1 --complement
Moreover, when specifying multiple characters/bytes/fields, the cut
command concatenates the output without a delimiter. Specify a delimiter in the output using the --output-delimiter
option.
For example, to set the output delimiter to _
(underscore), use:
cut -d',' -f1,3 --output-delimiter='_' example.csv
Combining cut with Other Commands
Combining the cut
command with other commands is a powerful way to manipulate and extract data in Linux.
For example, use ps
to list processes, then filter it with cut
to display specific columns, such as the process ID (PID) and the command name:
ps -e | cut -c1-5,25-
The command includes:
ps -e
. Displays all running processes.cut -c1-5,25-
. Extracts the PID (first five characters) and the command (from character 25 onwards).
Use cut
combined with head or tail to extract specific rows and columns from a file. For example, extract the first three rows and only the name and city fields (the 1st and 3rd columns). To do that, combine head
and cut
like this:
head -n 3 example.csv | cut -d ',' -f1,3
The command includes:
head -n 3 example.csv
. Outputs the first three lines from the example.csv file.cut -d ',' -f1,3
. Cuts out the first and third fields using a comma (,
) as the delimiter.
Linux cut: Handling Irregular Data Formats
The cut
command is designed for structured, regular data formats, and it struggles with irregular data where a single delimiter doesn't consistently separate fields. In such cases, cut
is not the best tool. However, there are ways to handle irregular data with some adjustments or by combining cut
with other tools.
For instance, if the data is separated by multiple spaces or inconsistent delimiters, use tr (translate) or sed to normalize the delimiters.
For example, the following file has data with irregular spacing:
Convert multiple spaces to a single space using tr -s
before cut
:
cat data.txt | tr -s ' ' | cut -d ' ' -f1,3
The command converts multiple spaces to a single space and then extracts the first and third fields.
Moreover, if your data uses mixed delimiters, such as spaces and commas, use sed
to convert all delimiters into a single format. For example, the following file contains data with both commas and spaces as delimiters:
To convert both commas and spaces to a single delimiter (e.g., a comma), run:
sed 's/ /,/g' data.txt | cut -d ',' -f1,3
The command converts spaces to commas and extracts the first and third fields.
Conclusion
This article explained what the Linux cut
command is and how to use it to process a file or command output. The guide also included a section on how to handle irregular data formats.
Next, learn about other important Linux commands with this Linux commands cheat sheet.