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
(bytes) 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
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.
Name of the input file. You may input multiple files. file not specified: Cutting out columns
(column) list possesses the format described under Format 1.
Name of the input file. You may name more than one file. file not specified: Cutting out fields
(field) list is specified as described under Format 1.
The character given as char serves as the field delimiter. -d not specified:
Lines with no field delimiters are suppressed. -s not specified:
Name of the input file. You may name more than one file. file not specified: |
Error
A line can have no more than 1023 characters or fields. Alternatively, the newline character may be missing.
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.
The delimiter character missing from the -d option.
Adjacent backspaces cannot be processed correctly.
|
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:
|
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.
|
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.
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:
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 |