Close Menu
    What's Hot

    YouTube Unblocked Proxy: Overview, Benefits, and Real-World Use Cases

    April 7, 2026

    Linux Kernel Release Frequency Statistics 2026

    April 7, 2026

    How To Use The SSH Login Command

    April 7, 2026
    Facebook X (Twitter) Instagram
    Command Linux
    • About
    • Man Pages
    • Arch Linux
    • Statistics
    • How to
      • Q&A
    • OS
      • Windows
    • Blog
      • Featured
    • MORE
      • Easter Eggs
      • IP Address
    • Write For Us
    • Contact Us
    Command Linux
    Home - How to - Bash Cut: How to Pull Out Portions of Text from Lines

    Bash Cut: How to Pull Out Portions of Text from Lines

    WillieBy WillieMarch 31, 2026Updated:April 1, 2026No Comments5 Mins Read

    The cut command reads lines from a file or standard input and prints specific portions based on fields, characters, or bytes. It works on one line at a time — no regex, no scripting — just fast column extraction at the terminal. If you’re new to working with text in the terminal, it’s one of the first tools worth learning.

    Bash cut Command Syntax

    The basic syntax takes an option and a file:

    cut [OPTION] [FILE]

    Without a file, cut reads from standard input, which makes it useful inside pipelines. Every invocation requires at least one of -f, -c, or -b. Calling cut without any option returns an error.

    bash cut Command Flags and Options

    Flag Long Form Purpose
    -f--fieldsSelect by field number (most common)
    -d--delimiterSet a custom input delimiter (default: tab)
    -c--charactersSelect by character position
    -b--bytesSelect by byte position
    --complement—Print everything except the selected fields
    -s--only-delimitedSkip lines with no delimiter
    --output-delimiter—Use a different character between output fields

    The cut man page lists the full specification for each flag, including edge cases for byte ranges in multibyte locales.

    Relative frequency of bash cut flag usage in shell scripts

    How to Extract a Single Column with bash cut

    Using the sample file example_data.txt:

    Kai     Refsnes  30,Norway
    Robin   Smith    25,Denmark
    Sienna  Davis    40,Germany

    Tab is the default delimiter, so pulling the first field needs nothing beyond -f1:

    cut -f1 example_data.txt
    Kai
    Robin
    Sienna

    Setting a Custom Delimiter with -d

    Tab separators don’t cover every case. CSV files, /etc/passwd, and many log formats use colons or commas. Pass -d with a single character to change the delimiter:

    cut -d',' -f1 example_data.txt
    Kai     Refsnes  30
    Robin   Smith    25
    Sienna  Davis    40

    With a comma as the boundary, everything before the first comma counts as column one. The -d flag accepts exactly one character — passing two or more triggers the error cut: delimiter must be a single character.

    Selecting Multiple or Range Columns

    The -f flag accepts ranges and comma-separated lists. Use 1-2 for a range and 1,3 for non-consecutive fields:

    cut -f1-2 example_data.txt
    Kai     Refsnes
    Robin   Smith
    Sienna  Davis

    To grab columns two through three:

    cut -f2-3 example_data.txt
    Refsnes  30,Norway
    Smith    25,Denmark
    Davis    40,Germany

    Range notation follows a simple pattern: N for a single field, N-M for a span, N- for everything from N to the end of the line.

    Using –complement to Exclude Fields

    Instead of listing what you want, --complement lets you list what to drop. It prints every field except the ones specified:

    cut --complement -f1 example_data.txt
    Refsnes  30,Norway
    Smith    25,Denmark
    Davis    40,Germany

    Column one is gone; everything else prints normally. The --complement flag is GNU-only and not available on macOS’s built-in BSD version of cut.

    Extracting by Character Position

    The -c flag works on character index rather than delimiters. It’s useful for fixed-width logs or data where columns aren’t separated by a character:

    cut -c1-3 example_data.txt
    Kai
    Rob
    Sie

    For multibyte characters (UTF-8 text), use -b instead to target exact byte positions rather than character counts.

    Using bash cut in Pipelines

    Because cut reads from standard input, it connects naturally with other commands via pipes. Pulling just the usernames from /etc/passwd takes one line:

    cut -d':' -f1 /etc/passwd

    Chaining with sort and uniq removes duplicates and alphabetizes the output in a single pass. The bash shell usage statistics for 2026 confirm that bash remains the dominant shell for scripting, making fluency with tools like cut practically universal in shell environments.

    cut -d':' -f1 /etc/passwd | sort | uniq

    When the output delimiter should differ from the input one, --output-delimiter handles the conversion without needing sed:

    cut -d',' -f1,3 --output-delimiter=':' example_data.txt

    Common Errors and How to Fix Them

    Error Cause Fix
    cut: delimiter must be a single character More than one character passed to -d Use exactly one character, e.g. -d','
    cut: fields and positions are numbered from 1 Zero used as a field number Start field numbering at 1, not 0
    Entire line printed instead of a field Delimiter not present in that line Add -s to skip lines without the delimiter
    Unexpected output on macOS --complement not supported in BSD cut Use awk or install GNU coreutils via Homebrew

    bash cut Quick Reference

    Task Command
    First column (tab-separated)cut -f1 file.txt
    Comma-separated, column onecut -d',' -f1 file.txt
    Columns two through threecut -f2-3 file.txt
    All columns except the firstcut --complement -f1 file.txt
    Characters 1 to 5 of each linecut -c1-5 file.txt
    Skip lines without the delimitercut -d':' -f1 -s file.txt
    Change output delimitercut -d',' -f1,2 --output-delimiter=':' file.txt
    Field from piped commandecho "a:b:c" | cut -d':' -f2

    FAQs

    What is the bash cut command used for?

    The cut command extracts specific fields, characters, or bytes from each line of a file or input stream. It’s commonly used to parse CSV files, log files, and command output in shell scripts.

    How do I use a custom delimiter with bash cut?

    Use the -d flag followed by a single character, such as cut -d',' -f1 file.txt for comma-separated data. The default delimiter is a tab character.

    What is the difference between -f, -c, and -b in cut?

    -f selects by field using a delimiter, -c selects by character position, and -b selects by byte position. For ASCII files all three behave similarly, but -b and -c differ for multibyte characters.

    Can I select non-consecutive fields with bash cut?

    Yes. Use a comma-separated list with -f, such as cut -f1,3 file.txt to print the first and third fields. For a continuous range, use -f1-3.

    How do I use bash cut with pipes?

    Pipe the output of another command directly into cut, for example: cat /etc/passwd | cut -d':' -f1. No file argument is needed when reading from standard input.

    Willie
    • Website

    Willie has over 15 years of experience in Linux system administration and DevOps. After managing infrastructure for startups and enterprises alike, he founded Command Linux to share the practical knowledge he wished he had when starting out. He oversees content strategy and contributes guides on server management, automation, and security.

    Related Posts

    How to Use WinSCP for Linux File Transfers

    March 30, 2026

    How to Resolve Exit Code 1 Errors in Kubernetes Containers

    March 28, 2026

    How to Check Memory Size in Linux Using Terminal and GUI

    March 28, 2026

    How to Linux List All Groups

    March 26, 2026
    Top Posts

    SFTP-SERVER

    January 28, 2026

    HEXDUMP

    March 7, 2026

    RECVMMSG

    February 19, 2026

    fiberlamp

    February 4, 2026
    • Home
    • Contact Us
    • Privacy Policy
    • Terms of Use

    Type above and press Enter to search. Press Esc to cancel.