cut
cut
is a command-line utility used to extract sections from each line of input, usually from a file. It is commonly used to extract specific columns or fields from text files.
Basic Syntax
Commonly Used Options
-b list
: Select only the bytes specified inlist
.-c list
: Select only the characters specified inlist
.-d delim
: Usedelim
as the field delimiter character instead of the default tab.-f list
: Select only the fields specified inlist
.-n
: Do not split multi-byte characters (used with-b
).--complement
: Complement the selection, i.e., select all bytes, characters, or fields except those specified.--output-delimiter=string
: Usestring
as the output delimiter.
Examples
Basic Usage
Extract the first 10 characters of each line in a file:
Extract Specific Fields
Extract the second and third fields from a file, using a comma as the field delimiter:
Extract Specific Bytes
Extract the first 5 bytes of each line in a file:
Complement Selection
Extract all fields except the first and second, using a tab as the field delimiter:
Using Output Delimiter
Extract the first and third fields and use a semicolon as the output delimiter:
Extract Fields with Multiple Delimiters
Extract the first field using a space as the delimiter:
Extract Fields from Standard Input
Extract the second field from the input provided by echo
:
Conclusion
cut
is a versatile tool for extracting specific sections from lines of text files. Understanding its options allows for efficient and powerful text extraction directly from the command line.