cut

text processinglinux
The cut command is one of the most frequently used commands in Linux/Unix-like operating systems. cut The cut command is used to extract sections from each line of input. It can extract by character position, field separated by a delimiter, or bytes.

Quick Reference

Command Name:

cut

Category:

text processing

Platform:

linux

Basic Usage:

cut [options] [arguments]

Common Use Cases

  • 1

    Text field extraction

    Extract specific fields from text data

  • 2

    Data processing

    Process structured data like CSV files

  • 3

    Log analysis

    Extract specific information from log files

  • 4

    Script automation

    Automate text processing in shell scripts

Syntax

cut OPTION... [FILE]...

Options

Option Description
-b, --bytes=LIST Select only the bytes in positions listed in LIST
-c, --characters=LIST Select only the characters in positions listed in LIST
-d, --delimiter=DELIM Use DELIM as the field delimiter character instead of TAB
-f, --fields=LIST Select only the fields listed in LIST, also print any line that contains no delimiter character, unless the -s option is specified
-n (ignored)
--complement Complement the set of selected bytes, characters, or fields
-s, --only-delimited Do not print lines not containing delimiters
--output-delimiter=STRING Use STRING as the output delimiter, the default is to use the input delimiter
-z, --zero-terminated Line delimiter is NUL, not newline
--help Display help message and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the cut command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Extract the first field from a colon-separated file
cut -d: -f1 /etc/passwd
Extract the first and third fields from a comma-separated file
cut -d, -f1,3 data.csv
Extract characters 1-10 from each line
cut -c1-10 file.txt
Extract characters 1, 3, and 5 from each line
cut -c1,3,5 file.txt

Advanced Examples:

Extract the username and home directory from /etc/passwd
cut -d: -f1,6 /etc/passwd
Extract the first field and suppress lines that don't contain the delimiter
cut -d: -f1 -s /etc/passwd
Extract the last field from a space-separated file
cut -d' ' -f-1 file.txt
Extract characters from position 5 to the end of each line
cut -c5- file.txt
Extract the first 3 characters from each line
cut -c-3 file.txt
Extract fields 2-4 from a tab-separated file
cut -f2-4 data.tsv
Extract the first field and complement (everything except first field)
cut -d: -f1 --complement /etc/passwd
Extract fields 1 and 3-5 from a comma-separated file
cut -d, -f1,3-5 data.csv

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Usage Notes:

The cut command allows you to work with three different selection methods:

  • Characters (-c): Select specific character positions from each line.
  • Bytes (-b): Select by byte position, which is important for multi-byte character sets and binary files.
  • Fields (-f): Select columns or fields based on a delimiter character.

Specifying LIST format:

When specifying which characters, bytes, or fields to extract, you can use these formats:

  • N: The Nth element (counting starts at 1).
  • N-M: From the Nth to the Mth element (inclusive).
  • N-: From the Nth element to the end of the line.
  • -M: From the first element to the Mth element.
  • N,M,...: Specific elements separated by commas.

Common Use Cases:

  • Extracting usernames or other specific fields from system configuration files.
  • Processing CSV files and extracting specific columns.
  • Formatting output by selecting only the needed parts of each line.
  • Working with fixed-width data files where fields have consistent positions.
  • Extracting specific portions of log files for analysis.

Limitations:

  • The cut command cannot handle variable-width fields unless they are properly delimited.
  • It doesn't support regular expressions for complex pattern matching (consider using awk or sed instead).
  • When used with -f option, cut doesn't trim whitespace around the delimiter by default.
  • Field selection doesn't work well with lines that don't contain the delimiter unless you use -s.

Common Use Cases

Text field extraction

Extract specific fields from text data

Data processing

Process structured data like CSV files

Log analysis

Extract specific information from log files

Script automation

Automate text processing in shell scripts

Data transformation

Transform data formats by extracting columns

Related Commands

These commands are frequently used alongside cut or serve similar purposes:

Use Cases

1

Text field extraction

Extract specific fields from text data

2

Data processing

Process structured data like CSV files

3

Log analysis

Extract specific information from log files

4

Script automation

Automate text processing in shell scripts

5

Data transformation

Transform data formats by extracting columns

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the cut command works in different scenarios.

$ cut
View All Commands