LinuxCommandLibrary

cut

Extract sections from lines of text

TLDR

Print the fifth character on each line

$ [command] | cut [[-c|--characters]] 5
copy

Print the fifth to tenth character of each line of the specified file
$ cut [[-c|--characters]] 5-10 [path/to/file]
copy

Split each line in a file by a delimiter into fields and print fields two and six (default delimiter is TAB)
$ cut [[-f|--fields]] 2,6 [path/to/file]
copy

Split each line by the specified delimiter and print all from the second field onward
$ [command] | cut [[-d|--delimiter]] "[delimiter]" [[-f|--fields]] 2-
copy

Use space as a delimiter and print only the first 3 fields
$ [command] | cut [[-d|--delimiter]] " " [[-f|--fields]] -3
copy

Do not print lines that do not contain the delimiter
$ [command] | cut [[-d|--delimiter]] "[:]" [[-f|--fields]] [1] [[-s|--only-delimited]]
copy

Print specific fields of lines that use NUL to terminate lines instead of newlines
$ [find . -print0] | cut [[-z|--zero-terminated]] [[-d|--delimiter]] "[/]" [[-f|--fields]] [2]
copy

SYNOPSIS

cut [-b|--bytes=LIST] [-c|--characters=LIST] [-f|--fields=LIST] [-d|--delimiter=DELIM] [-s|--only-delimited] [--output-delimiter=STRING] [--complement] [--help] [--version] [FILE...]

PARAMETERS

-b, --bytes=LIST
    select only these bytes

-c, --characters=LIST
    select only these characters

-d, --delimiter=DELIM
    use DELIM instead of TAB as field delimiter

-f, --fields=LIST
    select only these fields (ignore if no DELIM)

--complement
    complement the set of selected bytes/characters/fields

--output-delimiter=STRING
    use STRING as output delimiter (default: input DELIM)

-s, --only-delimited
    do not print lines without DELIM characters

--help
    display help and exit

--version
    output version information and exit

DESCRIPTION

cut is a standard Unix/Linux utility that removes (or "cuts") specified sections from each line of input files or standard input, outputting the results to standard output. It excels at parsing delimited text files like CSV, TSV, or system logs by selecting bytes (-b), characters (-c), or fields (-f).

Key features include:
- Field selection using a delimiter (default: TAB).
- Support for ranges, lists, or complements in selection lists (e.g., 1-3,5 or 2-).
- Custom output delimiters.
- Option to skip lines without delimiters (-s).

Typical uses: Extract usernames from /etc/passwd with cut -d: -f1 /etc/passwd; grab the first IP octet from logs; or slice fixed-width columns.

It reads from files or stdin, processes line-by-line without regex support, making it faster than awk or sed for simple extractions. However, it assumes uniform line structures and struggles with variable delimiters or quoted fields. For complex parsing, pair with sort or uniq. Always quote lists to avoid shell expansion.

CAVEATS

Byte/character modes (-b/-c) mishandle multi-byte UTF-8; use -f for Unicode. No regex/quoted field support. Fields numbered from 1; empty LIST errors out. Multiple -b/-c/-f combine selections.

LIST SYNTAX

Comma-separated: numbers (1), ranges (1-3, -3, 3-), lists (1,3-5). Repeatable; order preserved.

EXAMPLES

cut -d',' -f2 file.csv (2nd CSV column).
cut -c1-5,10- *.log (chars 1-5+10-).
echo "a:b:c" | cut -d: --fields=1 --complement (b:c)

HISTORY

Introduced in 7th Edition UNIX (1979) by AT&T; POSIX standardized in 1992. Coreutils version maintained by GNU since 1990s, with enhancements like --output-delimiter in GNU extensions.

SEE ALSO

awk(1), sed(1), paste(1), column(1), fmt(1)

Copied to clipboard