cut command (outputs selected parts of lines to the standard output)
- Organization of this page
Format
cut -b list [-n] [--output-delimiter=character-string] [path-name ...] cut -c list [--output-delimiter=character-string] [path-name ...] cut -f list [-s] [-d delimiter] [--output-delimiter=character-string] [path-name ...]
Description
This command outputs selected parts of lines to the standard output. For each line in the input files (or, by default, the standard input), the command selects the portions specified in list and outputs them to the standard output.
Arguments
If the -b, -c, and -f options are all omitted, the command outputs command usage information and terminates.
- --bytes=list
-
Specifies that what is to be cut and output is defined as a range of byte positions. The byte positions are specified in list (where a file's first byte is position 1). If multiple byte position ranges are specified, they are concatenated.
If this option is specified together with the --output-delimiter option, the character string specified in the --output-delimiter option is used to concatenate data.
- --characters=list
-
Specifies that what is to be cut and output is defined as a range of character positions. The character positions are specified in list (where a file's first character is position 1). If multiple character position ranges are specified, they are concatenated.
If this option is specified together with the --output-delimiter option, the character string specified in the --output-delimiter option is used to concatenate data.
- --fields=list
-
Specifies that what is to be cut and output is defined as a range of field positions. The field positions are specified in list (where a file's first field is position 1). If multiple field position ranges are specified, they are concatenated.
The selected fields are output separated by a delimiter. If the delimiter is not found in a line, the entire line is output, unless the -s option is specified, in which case lines that do not contain the delimiter are not output.
By specifying the --output-delimiter option, you can change the separator that is output together with the selected fields.
- list
-
Specifies either column positions or field positions as separated by the delimiter. Column positions start from 1.
You can specify multiple selection ranges by separating them with the comma, space, or tab. If you separate them with the space or tab, each selection range must be enclosed in double quotation marks ("). A selection range can be specified as n, x-, -y, or x-y, where n, x, and y are either field positions or column positions. No error results if a nonexistent position is specified.
-
n: Specifies a single position that is to be output.
-
x-: Specifies that all positions beginning with position x through the end of the file are to be output.
-
-y: Specifies that all positions from the beginning of the file through position y are to be output.
-
x-y: Specifies that all positions beginning with position x through position y are to be output. In such a case, x < y. If x > y, an error message (cut: [-bcf] list: illegal list value) is output.
-
- -n
-
Specifies that multibyte characters are not to be split. If this option is not specified, the -b option results in multibyte characters being split into separate bytes.
- path-name
-
Specifies the path name of a file that is to be read as the input. If path-name is omitted or a hyphen (-) is specified, the standard input is read.
- --only-delimited
-
Specifies that lines that do not contain a delimiter are not to be output. If this option is omitted when the -f option is specified, the command will only display command usage information and terminate.
- --delimiter=delimiter
-
Specifies the field delimiter to be used (only the initial character of the specified value is used as the delimiter). If this option is omitted, the field delimiter is set to the tab.
If this option is omitted when the -f option is specified, the command will only display command usage information and terminate.
- --output-delimiter=character-string
-
When this option is specified together with the -f option, specifies the replacement character string that is to be used as the separator for the fields that are to be output.
When this option is specified together with the -b or -c option, specifies the replacement character string that is to be used to concatenate fields.
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination |
1 |
Error termination |
Notes
-
The cut command expects text files. Input from binary files and output of binary data are not guaranteed to work.
Usage examples
These usage examples illustrate the results of executing the cut command on the file test.txt, whose contents are shown in the following:
123:5678:abcdef:hijkl field1:field2:field3:filed4 sssssssssssssssssssssss
This file is used as the input file in the following examples, which illustrate the results of executing the cut command.
-
Output the first byte and the third through fifth bytes.
$ cut -b 1,3-5 test.txt 13:5 feld ssss
-
Output the first through fourth characters.
$ cut -c -4 test.txt 123: fiel ssss
-
Output the first and fourth fields.
$ cut -f 1,4 -d : test.txt 123:hijkl field1:filed4 sssssssssssssssssssssss
-
Output the first byte and the third through fifth bytes with character string @:/ added between fields.
$ cut -b 1,3-5 --output-delimiter="@:/" test.txt 1@:/3:5 f@:/eld s@:/sss
-
Display the first and fourth fields and replace the separator with the character string @:/.
$ cut -f 1,4 -d : --output-delimiter="@:/" test.txt 123@:/hijkl field1@:/field4 sssssssssssssssssssssss
-
Output an option error message.
This message might vary depending on the platform on which the command is executed. The following shows an example in Windows:
C:\TEMP>%ADSH_OSCMD_DIR%\cut -z cut: illegal option -- z usage: cut -b list [-n] [--output-delimiter=string] [file ...] cut -c list [--output-delimiter=string] [file ...] cut -f list [-s] [-d delim] [--output-delimiter=string] [file ...]