$linuxjunkies
>

cut(1)

Remove sections from each line of files or stdin.

UbuntuDebianFedoraArch

Synopsis

cut [OPTION]... [FILE]...

Description

The cut command removes (cuts out) sections from each line of input based on character position, byte position, or field delimiter. It's commonly used to extract columns from structured text like CSV files, output from other commands, or log files.

By default, cut uses a tab character as the field delimiter. You can specify a different delimiter with -d and select which fields to keep with -f. Alternatively, select by character position with -c or byte position with -b.

Common options

FlagWhat it does
-f LISTSelect only these fields (comma or range separated, e.g., 1,3 or 1-3)
-d DELIMUse DELIM as the field separator instead of tab
-c LISTSelect only these character positions (1-indexed, e.g., 1-5,10)
-b LISTSelect only these byte positions
--complementInvert selection; print all fields/characters except those specified
-sSkip lines that don't contain the delimiter (used with -f)
-zUse NUL (null character) as the line terminator instead of newline
--output-delimiter=STRINGUse STRING for output field separator (default: same as input)

Examples

Extract the 1st and 3rd fields (username and UID) from /etc/passwd using ':' as delimiter

cut -f 1,3 /etc/passwd

Extract fields 2 and 4 from a comma-separated CSV file

cut -d',' -f 2,4 data.csv

Display characters 1-20 and 72 onward from ps output (user and command columns)

ps aux | cut -c 1-20,72-

Extract username, full name (GECOS), and shell from passwd file

cut -d: -f1,5,7 /etc/passwd

Print first 5 characters of a string ('hello')

echo 'hello world' | cut -c 1-5

Extract first 3 tab-separated fields and output with pipe delimiter

cut -f 1-3 --output-delimiter='|' data.tsv

Extract specific space-separated columns from a log file

cut -d' ' -f2,4,6 logfile.txt

Extract all fields from field 2 to the end of each line

cut -f 2- data.txt

Related commands