Linux cut Command: Extract Characters and Fields

cut selects and outputs specified bytes, characters, or delimiter-based fields from each input line. It is convenient for simple column extraction, but it is not a complex CSV parsing tool.

The field mode -f uses a tab as the default delimiter. To use a colon or a comma, specify -d as well. Choose -c or -b considering implementation and character encoding.

Basic syntax

cut -d 'delimiter' -f field_number [file...]

Installed implementations and options may vary depending on the distribution. Check the description for your system with man cut.

Examples

Extracting colon-delimited fields

Extract the first field from the system account list.

cut -d ':' -f 1 /etc/passwd

Selecting multiple fields

Fields specified with commas are output in the order of the original input.

cut -d ':' -f 1,7 /etc/passwd

Extract character range

Used when looking at the beginning of each line.

cut -c 1-8 sample.txt

Results of extracting fields using a delimiter

This is an example of extracting only the first field from two lines separated by a colon. -d is the delimiter, and -f is the field number to select.

printf 'alpha:100\nbeta:200\n' | cut -d: -f1

Example output:

alpha
beta

cut -d ' ' -f2 cannot reliably parse text with multiple consecutive spaces by columns. For such data, awk is suitable. Formats where a delimiter can appear inside quotes, like CSV, cannot be accurately handled by cut alone, so use a dedicated CSV parser.

Main Options and Format

Options/Format Description
-d delimiter Specify the field separator character.
-f list Selects the field number or range to output.
-c list Selects character positions.
-b list Select byte positions.
-s Do not print lines without field delimiters.

Precautions when using

cut -d ',' does not understand commas inside quotes or multi-line CSV fields. For such data, use a CSV-specific parser. Lines without delimiters are printed as is by default, so add -s if necessary.

Frequently Asked Questions

Can multiple columns separated by spaces also be extracted with cut?

If the width of consecutive spaces is inconsistent, the field numbers may be misaligned. Such input is usually better handled by awk.

Official Documentation

You can check the exact behavior of options and implementation differences in the official cut documentation.