cut (Unix)

From Wikipedia, the free encyclopedia

Template:Short description

Script error: No such module "For".

Page Module:Infobox/styles.css has no content.

cut
[[Programmer|Original authorTemplate:Pluralize from text]]AT&T Bell Laboratories
[[Programmer|DeveloperTemplate:Pluralize from text]]Various open-source and commercial developers
Initial releaseFebruary 1985; 41 years ago (1985-02)
Template:Infobox software/simple
Operating systemUnix, Unix-like, IBM i
PlatformCross-platform
TypeCommand
Licensecoreutils: GPLv3+
WebsiteScript error: No such module "WikidataIB".

Script error: No such module "Check for conflicting parameters".

cut is a shell command that extracts sections from each line of input text — usually from a file. 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.

History

The original Bell Labs version was written by Gottfried W. R. Luderer.[1][2] The command 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.[3] It first appeared in AT&T System III UNIX in 1982.[4]

The command is commonly available on Unix and Unix-like operating systems. It is part of the BSD Base System. The version in GNU coreutils was written by David M. Ihnat, David MacKenzie, and Jim Meyering.[5] The command is available for Windows via UnxUtils.[6] The command was ported to the IBM i operating system.[7]

Use

The command line consists of options and an optional file path. If no path is specified than standard input will be used.

Options include:

Page Template:Mono/styles.css has no content.-b
Bytes; a list following Page Template:Mono/styles.css has no content.-b specifies a range of bytes which will be returned, e.g. cut -b1-66 would return the first 66 bytes of a line. NB If used in conjunction with Page Template:Mono/styles.css has no content.-n, no multi-byte characters will be split.
Page Template:Mono/styles.css has no content.-c
Characters; a list following Page Template:Mono/styles.css has no content.-c specifies a range of characters which will be returned, e.g. cut -c1-66 would return the first 66 characters of a line
Page Template:Mono/styles.css has no content.-f
Specifies a field list, separated by a delimiter
list
A comma separated or blank separated list of integer denoted fields, incrementally ordered. The Page Template:Mono/styles.css has no content.- indicator may be supplied as shorthand to allow inclusion of ranges of fields e.g. Page Template:Mono/styles.css has no content.4-6 for ranges 4–6 or Page Template:Mono/styles.css has no content.5- as shorthand for field 5 to the end, etc.
Page Template:Mono/styles.css has no content.-n
Used in combination with -b suppresses splits of multi-byte characters
Page Template:Mono/styles.css has no content.-d
Delimiter; the character immediately following the Page Template:Mono/styles.css has no content.-d option is the field delimiter for use in conjunction with the Page Template:Mono/styles.css has no content.-f 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.
Page Template:Mono/styles.css has no content.-s
Bypasses lines which contain no field delimiters when Page Template:Mono/styles.css has no content.-f is specified, unless otherwise indicated.

Examples

Given a file named foo with content:

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:

$ cut -c 4-10 foo
:bar:ba
:two:th
ha:beta
 quick

To output the fifth field through the end of the line of each line using the colon character as the field delimiter:

$ cut -d ":" -f 5- foo
quux
five:six:seven
epsilon:zeta:eta:theta:iota:kappa:lambda:mu
the quick brown fox jumps over the lazy dog

Because the colon 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:

$ cut -d " " -f 3 foo
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

Because the space character is not found in the first three lines these entire lines are shown.

To separate two words having any delimiter:

$ line=process.processid
$ cut -d "." -f1 <<< $line
process
$ cut -d "." -f2 <<< $line
processid

See also

References

Page Template:Reflist/styles.css has no content.

  1. ^ Page Module:Citation/CS1/styles.css has no content."cut(1) - OpenBSD manual pages".
  2. ^ Page Module:Citation/CS1/styles.css has no content."[TUHS] A portrait of cut(1)". 15 January 2020. Archived from the original on 6 April 2023. Retrieved 6 April 2023.
  3. ^ Template:Man
  4. ^ Template:Man
  5. ^ Template:Man
  6. ^ Page Module:Citation/CS1/styles.css has no content."Native Win32 ports of some GNU utilities". unxutils.sourceforge.net. Archived from the original on 2006-02-09. Retrieved 2025-08-09.
  7. ^ Page Module:Citation/CS1/styles.css has no content.IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Archived (PDF) from the original on 2020-09-18. Retrieved 2020-09-05.

Page Module:Side box/styles.css has no content.Page Template:Sister project/styles.css has no content.

Lua error in package.lua at line 80: module 'Module:Navbox/configuration' not found. Template:Core Utilities commands