Linux and Unix cut command

Quick links

About cut
Syntax
Examples
Related commands
Linux and Unix main page

About cut

Remove or "cut out" sections of each line of a file or files.

Syntax

cut OPTION... [FILE]...

Options

-b, --bytes=LIST Select only the bytes specified in LIST.
-c, --characters=LIST Select only the characters specified in LIST
-d, --delimiter=DELIM use DELIM instead of a tab for the field delimiter
-f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified
-n This option is ignored, but is included for compatibility reasons.
--complement complement the set of selected bytes, characters or fields.
-s, --only-delimited do not print lines not containing delimiters.
--output-delimiter=STRING use STRING as the output delimiter string. The default is to use the input delimiter.
--help Display a help message and exit.
--version output version information and exit.

Usage Notes

When invoking cut, use the -b, -c, or -f option, but only one of them.

Each LIST is made up of a range, or multiple ranges separated by commas. Selected input is written in the same order that it is read, and is written to output exactly once. Each range is one of:

N the Nth byte, character, or field, counted from 1.
N- from the Nth byte, character, or field, to the end of the line.
N-M from the Nth to the Mth byte, character, or field (inclusive).
-M from the first to the Mth byte, character, or field.

If no FILE is specified, cut reads from the standard input.

Examples

cut -c3 file.txt

Outputs the third character of every line of the file file.txt, cutting out the others.

cut -c1-3 file.txt

Outputs the first three characters of every line of the file file.txt, cutting out the rest.

cut -c3- file.txt

Outputs the third through the last characters of each line of the file file.txt, cutting out the firs two characters.

cut -c- file.txt

Outputs all of every line of the file file.txt, cutting out nothing.

cut -d':' -f1 /etc/passwd

Outputs the first field of the file /etc/passwd, where fields are delimited by a colon (':'). The first field of /etc/passwd is the username, so this command will output every username in the passwd file.

grep "/bin/bash" /etc/passwd | cut -d':' -f1,6

Outputs the first and sixth fields, delimited by a colon, of any entry in the /etc/passwd file which specifies /bin/bash. This command will output the username and home directory of any user whose login shell is /bin/bash.

Related commands

grep
paste