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

    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 characters 1-5 from each line
    cut -c 1-5 filename.txt
    # Extract the first field (column) using tab as delimiter cut -f 1 filename.txt
    # Extract the first and third fields using colon as delimiter cut -d ':' -f 1,3 /etc/passwd
    # Extract fields 1 through 3 using comma as delimiter cut -d ',' -f 1-3 data.csv

    Advanced Examples:

    # Combine cut with other commands
    
    cat /etc/passwd | cut -d ':' -f 1,7 | sort
    # Extract all fields except the second cut -d ',' -f 1,3- data.csv # Extract characters from the 10th position to the end of each line cut -c 10- filename.txt # Extract the first 8 bytes from a binary file cut -b 1-8 binary_file # Use a different output delimiter cut -d ':' -f 1,3,6 --output-delimiter=',' /etc/passwd

    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.

    Related Commands

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

    Use Cases

    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