Cut (Unix)
Template:Short descriptionTemplate:Lowercase title
Template:For
{{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1
| unknown = Template:Main other
| preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y
| AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid
}}Template:Main other
In computing, cut
is a command line utility on Unix and Unix-like operating systems which is used to extract sections from each line of input — usually from a file. It is currently part of the GNU coreutils package and the BSD Base System.
Extraction of line segments can typically be done by bytes (-b
), characters (-c
), or fields (-f
) separated by a delimiter (-d
— the tab character by default). A range must be provided in each case which consists of one of N
, N-M,
N-
(N
to the end of the line), or -M
(beginning of the line to M
), where N and M are counted from 1 (there is no zeroth value). Since version 6, an error is thrown if you include a zeroth value. Prior to this the value was ignored and assumed to be 1.
HistoryEdit
The original Bell Labs version was written by Gottfried W. R. Luderer.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> <syntaxhighlight lang="text" class="" style="" inline="1">cut</syntaxhighlight> is part of the X/Open Portability Guide since issue 2 of 1987. It was inherited into the first version of POSIX.1 and the Single Unix Specification.<ref>Template:Man</ref> It first appeared in AT&T System III UNIX in 1982.<ref>Template:Man</ref>
The version of cut
bundled in GNU coreutils was written by David M. Ihnat, David MacKenzie, and Jim Meyering.<ref>Template:Man</ref> The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.<ref>{{#invoke:citation/CS1|citation
|CitationClass=web
}}</ref> The Template:Mono command has also been ported to the IBM i operating system.<ref>{{#invoke:citation/CS1|citation
|CitationClass=web
}}</ref>
ExamplesEdit
Assuming a file named "file
" containing the lines:
foo:bar:baz:qux:quux one:two:three:four:five:six:seven alpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mu the quick brown fox jumps over the lazy dog
To output the fourth through tenth characters of each line: <syntaxhighlight lang="console"> $ cut -c 4-10 file
- bar:ba
- two:th
ha:beta
quick
</syntaxhighlight> To output the fifth field through the end of the line of each line using the colon character as the field delimiter: <syntaxhighlight lang="console"> $ cut -d ":" -f 5- file quux five:six:seven epsilon:zeta:eta:theta:iota:kappa:lambda:mu the quick brown fox jumps over the lazy dog </syntaxhighlight>
(note that because the colon character is not found in the last line the entire line is shown)
Option -d
specifies a single character delimiter (in the example above it is a colon) which serves as field separator. Option -f
which specifies range of fields included in the output (here fields range from five till the end). Option -d
presupposes usage of option -f
.
To output the third field of each line using space as the field delimiter: <syntaxhighlight lang="console"> $ cut -d " " -f 3 file foo:bar:baz:qux:quux one:two:three:four:five:six:seven alpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mu brown </syntaxhighlight> (Note that because the space character is not found in the first three lines these entire lines are shown.)
To separate two words having any delimiter: <syntaxhighlight lang="console"> $ line=process.processid $ cut -d "." -f1 <<< $line process $ cut -d "." -f2 <<< $line processid </syntaxhighlight>
SyntaxEdit
cut [-b list] [-c list] [-f list] [-n] [-d delim] [-s] [file]
Flags which may be used include:
- Template:Mono
- Bytes; a list following Template:Mono specifies a range of bytes which will be returned, e.g. <syntaxhighlight lang="text" class="" style="" inline="1">cut -b1-66</syntaxhighlight> would return the first 66 bytes of a line. NB If used in conjunction with Template:Mono, no multi-byte characters will be split. NNB. Template:Mono will only work on input lines of less than 1023 bytes
- Template:Mono
- Characters; a list following Template:Mono specifies a range of characters which will be returned, e.g. <syntaxhighlight lang="text" class="" style="" inline="1">cut -c1-66</syntaxhighlight> would return the first 66 characters of a line
- Template:Mono
- Specifies a field list, separated by a delimiter
- list
- A comma separated or blank separated list of integer denoted fields, incrementally ordered. The Template:Mono indicator may be supplied as shorthand to allow inclusion of ranges of fields e.g. Template:Mono for ranges 4–6 or Template:Mono as shorthand for field 5 to the end, etc.
- Template:Mono
- Used in combination with -b suppresses splits of multi-byte characters
- Template:Mono
- Delimiter; the character immediately following the Template:Mono option is the field delimiter for use in conjunction with the Template:Mono option; the default delimiter is tab. Space and other characters with special meanings within the context of the shell in use must be enquoted or escaped as necessary.
- Template:Mono
- Bypasses lines which contain no field delimiters when Template:Mono is specified, unless otherwise indicated.
- file
- The file (and accompanying path if necessary) to process as input. If no file is specified then standard input will be used.
See alsoEdit
ReferencesEdit
External linksEdit
- Template:Man
- Softpanorama cut page.
- Cut out selected fields of each line of a file A portrait of cut(1) and its historical background.