Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

cut - cut out selected fields of each line of a file

&pagelevel(4)&pagelevel

cut reads the input text line by line from files or from the standard input and cuts out of each line selected bytes (Format 1), characters (Format 2) or fields (Format 3). cut outputs the removed components at the standard output.


Syntax


Format 1: cut -b list[ -n][ file ...]
Format 2: cut -c list[ file ...]
Format 3: cut -f list[ -d char][ -s][ file ...]



Cutting out bytes
Format 1: cut -b list[ -n][ file ...]


-b list

(bytes)
cut cuts out of each input line the bytes which are in the position specified in list and sends them to the standard output.

list is a list of numbers or numerical ranges. The elements in the list must be separated by commas and arranged in ascending order. The range n1-n2 refers to all the numbers between n1 and n2 inclusive.

The following abbreviations can be used to specify ranges:

-n

for 1- n

n- for

n-"last column"

Example


1,3,5cut cuts the 1st, 3rd and 5th columns.
1-3,5cut cuts the 1st, 2nd, 3rd and 5th columns.
-3,5is an abbreviation for 1-3,5
3-is an abbreviation for 3-"last column"


-n

A character which may consist of multiple bytes is not cut out byte by byte. Every list character which is specified in the range n1-n2 is treated as follows:

If n1 is not the first byte of the character, then n1 is ignored in order to cut the first byte of the character.
If n2 is not the last byte of the character, then n2 is ignored. The last character to
precede n2 is selected. n2 is reset to zero if there is no preceding character.

file

Name of the input file. You may input multiple files.

file not specified:
cut reads from the standard input.


Cutting out columns
Format 2: cut -c list[ file ...]


-c list

(column)
The columns specified in list are cut out from each input line and written to standard output. A column is exactly one character wide.

list possesses the format described under Format 1.

file

Name of the input file. You may name more than one file.

file not specified:
cut reads from standard input.


Cutting out fields
Format 3: cut -f list[ -d char][ -s][ file ...]


-f list

(field)
cut cuts out the fields specified in list from each input line and writes them on standard output.
A field comprises all characters that are located between two field delimiters. Two successive field delimiters mark an empty field. The field delimiter defaults to the tab character, but you can redefine it in the -d option.
The output fields are separated from one another by one field delimiter each. Input lines with no field delimiters are output in full (unless the -s option is set). This is generally useful for table subheadings.

list is specified as described under Format 1.

-d char

The character given as char serves as the field delimiter.
If char is a blank or a shell metacharacter (see section “Metacharacters for the POSIX shell”) it must be enclosed in single quotes: -d’char’.

-d not specified:
The field delimiter defaults to the tab character.

-s

Lines with no field delimiters are suppressed.

-s not specified:
Lines with no field delimiters are output in full.

file

Name of the input file. You may name more than one file.

file not specified:
cut reads from standard input.

Error

ERROR: line too long

A line can have no more than 1023 characters or fields. Alternatively, the newline character may be missing.


cut: Bad list for b/c/f option

Incorrect list specification or missing option -b, -c or -f. No error is reported if the line possesses fewer fields than are required by list.


cut: No delimiter

The delimiter character missing from the -d option.


ERROR: cannot handle multiple adjacent backspaces

Adjacent backspaces cannot be processed correctly.


cut: Cannot open file
The named file cannot be read or does not exist. If multiple files have been specified, processing will continue on the other files.

Locale

The following environment variables affect the execution of cut:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determines the internationalized environment for the interpretation of byte sequences as characters (e.g. single-byte characters as opposed to multibyte characters in arguments and input files).

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

Print the first 72 characters of each input line:

$ cut -c1-72 file

Example 2

The first and third fields of the /etc/group file (the group name and group number) are cut out and output. The individual fields in this file are separated by means of a colon.

$ cut -f1,3 -d: /etc/group

Example 3

The names of customers whose bills are due on the first day of any month are to be filtered out of the invoice file of a mail order house along with the amount due from each of them.

The file is structured as follows:

Homewood        Milwaukee         10,000        06.01.05
Mackenzie       Detroit            7,000        07.06.05
Macnamara       Boston             8,000        05.01.05
Tinniswood      Atlanta              450        06.20.05

The fields in the table are separated by exactly one tab and padded with blanks.

$ grep '\.01\.05' invoice    | cut -f1,3 > names
$ cat names
Homewood        10,000
Macnamara        8,000

Explanation: grep finds all lines containing the string .01.05 and writes them to standard output. cut receives these lines as input, selects the first and third fields, and writes its output to the file names.

If you would then like each customer name in the names file to be preceded by the date and then sorted accordingly, you could enter:

$ grep '\.01\.05' invoice | cut -f4 > date

$ paste date names | sort

05.01.05        Macnamara        8,000
06.01.05        Homewood        10,000

Explanation: The first command line writes the date associated with the selected customer names to the file named date. The paste command pastes the lines in the date and names files horizontally, separating them with the default tab character. The sort command then sorts the output lines in ascending order of date.

See also

awk, grep, paste