VMware ESXi and vSphere Cluster Management

Filter Text with the Linux cut Command

Learn how to use Linux cut to extract character positions and delimiter-separated fields from files, standard input, and pipelines.

The Linux cut command is a simple text-filtering utility. It reads text one line at a time and outputs only selected character positions or delimiter-separated fields from each line.

Use cut when the data has a predictable layout: fixed-width records, tab-separated columns, or records separated by one known character such as a colon or slash. It can read named files or receive text through standard input.

Basic Command Structure

The general syntax is:

cut [options] [file...]

An input-selection option is required. The most common choices are -c for character positions and -f for fields:

cut -c LIST file
cut -f LIST file

Here, LIST describes the positions to extract. Each input line normally produces one result line. If no file is supplied, cut reads standard input, which may come from a pipeline or an interactive stream.

Common cut Options

OptionPurposeExample
-c LISTSelect character positions.cut -c 2-5 file
-f LISTSelect delimiter-separated fields.cut -f 2 file
-d DELIMITERUse a specified single-character field delimiter instead of a tab.cut -d ':' -f 1 file
--output-delimiter=STRINGChange the separator printed between selected fields.cut -d ':' --output-delimiter=' | ' -f 1,2 file
-sSuppress lines that do not contain the delimiter in field mode.cut -s -d ':' -f 1 file

Selecting Characters with -c

A character position is a numbered location within a line. With -c, positions are counted from the left, beginning at 1, not 0.

For example, if results.txt contains one record per line, this command prints the third character from every line:

cut -c 3 results.txt

Use a hyphen to specify an inclusive character range. This prints characters 2, 3, 4, 5, and 6 from each line:

cut -c 2-6 results.txt

You can also select separate positions with a comma-separated list:

cut -c 1,4,8 results.txt

Open-ended ranges continue to the beginning or end of the line:

cut -c -4 results.txt
cut -c 5- results.txt

The first command selects positions 1 through 4. The second selects position 5 through the end of each line.

Character and Field List Syntax

List formMeaningExample
NOne position or field.3
N-MAn inclusive range.2-6
N,MMultiple individual selections.1,4,8
-MFrom the beginning through position or field M.-4
N-From position or field N through the end.5-

Selecting Fields with -f

A field is one section of a line separated from the next section by a delimiter. A delimiter is a character such as a tab, slash, colon, or comma.

Field mode is intended for consistently delimited data. Fields are numbered from left to right, beginning with field 1.

To select one field, use its number after -f:

cut -f 2 results.txt

This prints the second field from each tab-delimited line. Select several fields with a comma-separated list:

cut -f 1,3 results.txt

Select a consecutive range with a hyphen:

cut -f 1-3 results.txt

When multiple fields are selected, cut normally retains the delimiter between the selected fields in its output.

The Default Tab Delimiter

In field mode, cut treats the tab character as the default delimiter. Therefore, a tab-separated file does not need a -d option.

cut -f 2 results.txt
cut -f 1,3 results.txt

These commands select fields from tab-delimited records. A visible gap between columns is not enough to prove that a file contains tabs; the file may instead contain spaces. Inspect the actual input format when results seem wrong.

Changing the Delimiter with -d

Use -d followed by one delimiter character to process records that are not tab-separated. The delimiter must be a single character.

Slash-Separated Records

For input such as department/team/member, use a slash as the delimiter:

cut -d '/' -f 3 results.txt

This outputs the third slash-separated field from every line.

Colon-Separated Records

Colon-separated account-style records can be processed like this:

cut -d ':' -f 1 users.txt

This outputs the first field from each record, such as a username.

Comma-Separated Records

For simple comma-separated data, specify a comma:

cut -d ',' -f 2 data.txt

Shell quoting makes delimiter intent clear and protects characters that may have special meaning to the shell. For example, quote delimiters such as ':', '/', or ' ' when using a space. The delimiter passed to -d is still only one character.

Using cut in Pipelines

A pipeline uses the | operator to pass one command's output to another command. The receiving command gets that output through standard input, meaning input supplied directly by a pipeline or stream rather than a named file.

printf 'alice:1001:/home/alice\n' | cut -d ':' -f 1,3

The output is:

alice:/home/alice

Here, printf produces a colon-separated record. cut narrows it to the username and home-directory fields.

A file-display command can also provide input:

cat results.txt | cut -c 2-6

For a single file, supplying the file directly is often simpler:

cut -c 2-6 results.txt

Output Delimiters and Lines Without Delimiters

When selecting multiple fields, cut normally uses the input delimiter between selected fields. GNU cut also supports --output-delimiter when you want a different output separator:

printf 'alice:1001:/home/alice\n' | cut -d ':' --output-delimiter=' | ' -f 1,3

Field mode normally passes through a line that does not contain the selected delimiter. Add -s to suppress such lines:

cut -s -d ':' -f 1 users.txt

Use -s only when ignoring delimiter-free lines is the desired behavior.

Choosing the Right Selection Mode

Input formatRecommended cut optionReason
Fixed-width lines-cValues occupy known character positions.
Tab-separated records-fThe default delimiter is a tab.
Single-character custom-delimited records-d CHAR -fA known separator identifies each field.
Quoted CSV or irregular whitespace-separated textAnother suitable toolcut does not understand CSV quoting or flexible whitespace rules.

Limits and Suitable Use Cases

Fixed-Width Data

Character mode is useful when every record has a stable layout, such as an identifier whose meaningful portion always occupies positions 2 through 6. It is less suitable when columns are separated by variable spacing or when visual alignment depends on tabs.

Predictable Delimiters

Field mode works best when every record uses one predictable, one-character separator. It is not a general-purpose parser for arbitrary structured data.

Quoted CSV

cut -d ',' treats every comma as a separator. It does not understand CSV quoting, so a value such as "New York, NY" can be split incorrectly. Use a CSV-aware parser when commas may occur inside quoted values.

Whitespace-Separated Text

Repeated spaces are separate delimiter characters to cut. As a result, splitting on a space can create empty or unexpected fields when spacing varies. Normalize the input first or use awk, which is designed for whitespace-aware and rule-based column processing.

Troubleshooting

  • The expected field is not extracted: The file may not be tab-delimited. Identify its actual separator and provide it with -d, such as cut -d ':' -f 1 file.
  • The field number appears off by one: Count the first field as field 1. Character positions also begin at 1.
  • Character output does not align with visible columns: The file may contain tabs, variable-width spacing, or multibyte characters rather than fixed-width positions. Use field selection with the correct delimiter or choose a more suitable tool.
  • Splitting on spaces produces unexpected fields: Repeated spaces are separate delimiters. Normalize the input or use awk for whitespace-separated columns.
  • Comma-separated values are split incorrectly: A comma may occur inside quoted text. Use a CSV-aware parser instead of cut.
  • Some lines appear unchanged: Those lines may not contain the selected delimiter. Inspect the input, or use -s to suppress lines without the delimiter.

Quick Reference

# Third character from every line
cut -c 3 results.txt

# Characters 2 through 6
cut -c 2-6 results.txt

# Nonadjacent character positions
cut -c 1,4,8 results.txt

# Second tab-delimited field
cut -f 2 results.txt

# First through third tab-delimited fields
cut -f 1-3 results.txt

# Third slash-delimited field
cut -d '/' -f 3 results.txt

# First colon-delimited field
cut -d ':' -f 1 users.txt

# Select fields from standard input
command-producing-records | cut -d ':' -f 1,3

For related text-processing tasks, see Linux text filtering.