| title | Linux Command Tutorial: cut | ||||
|---|---|---|---|---|---|
| date | 2026-09-12 00:00:00 +0000 | ||||
| categories |
|
||||
| tags |
|
||||
| draft | false | ||||
| slug | linux-cut-tutorial | ||||
| description | Authoritative reference tutorial for cut (GNU Coreutils), detailing column slicing by bytes (-b), characters (-c), and fields (-f), delimiter handling, and POSIX portability. | ||||
| upstream_suite | gnu-coreutils | ||||
| upstream_version | GNU Coreutils 9.11 | ||||
| posix_standard | POSIX.1-2024 | ||||
| research_date | 2026-09-12 |
The Linux Command Tutorial series provides rigorous, upstream-verified references for essential system commands across Linux distributions and UNIX-like environments. Each article focuses on a single executable, combining exhaustive option documentation, verified real-world examples, security boundaries, and best practices directly derived from official source documentation and POSIX standards.
Upstream: GNU Coreutils 9.11 | POSIX: POSIX.1-2024 (with GNU extensions) | Safety Tier: safe-read-only | Scope: text-processing
cut removes sections from each line of files or standard input. It extracts column fields based on delimiters (e.g. commas, tabs, colons) or slices exact byte/character offsets.
- Upstream Project & Provenance: Distributed in GNU Coreutils (
coreutils). - Portability & Standards Baseline: Standardized in IEEE Std 1003.1-2024 (POSIX.1-2024). GNU
cutintroduces custom output delimiters (--output-delimiter), complement ranges (--complement), and zero-terminated records (-z). - Target Research Implementation: Audited against GNU Coreutils 9.11 (
cut(1)). - Applicability & Lifecycle: The lightweight choice for slicing delimited files (CSV, TSV,
/etc/passwd) without spawning heavy language runtimes.
cut OPTION... [FILE]...cut requires specifying exactly one extraction mode:
- Bytes (
-b LIST): Selects byte offsets (1-indexed). - Characters (
-c LIST): Selects character offsets (multi-byte safe in UTF-8). - Fields (
-f LIST): Selects delimited fields separated by-d.
| Short Flag | Long Flag | Description | POSIX Defined |
|---|---|---|---|
-b LIST |
--bytes=LIST |
Select only these bytes. | Yes |
-c LIST |
--characters=LIST |
Select only these characters. | Yes |
-d CHAR |
--delimiter=CHAR |
Use CHAR instead of TAB for field delimiter. | Yes |
-f LIST |
--fields=LIST |
Select only these fields. | Yes |
-s |
--only-delimited |
Do not print lines not containing delimiters. | Yes |
| N/A | --complement |
Complement the set of selected bytes, characters or fields. | No |
| N/A | --output-delimiter=STRING |
Use STRING as the output delimiter (default is input delimiter). | No |
-z |
--zero-terminated |
Line delimiter is NUL (\0), not newline. |
No |
| Operation | Command | Notes |
|---|---|---|
| Extract 1st column (colon-delimited) | cut -d: -f1 /etc/passwd |
Slices field 1 using delimiter : |
| Extract multiple fields | cut -d, -f1,3 data.csv |
Extracts 1st and 3rd fields |
| Extract field range | cut -d$'\t' -f2-4 table.tsv |
Extracts fields 2 through 4 tab-delimited |
| Slice fixed-width characters | cut -c 1-10 fixed.txt |
Extracts characters 1 through 10 |
| Invert field selection | cut -d, --complement -f2 data.csv |
Drops column 2, keeping all other fields |
| Change output delimiter | cut -d: -f1,7 --output-delimiter=" " /etc/passwd |
Replaces delimiter in output stream |
| Suppress non-delimited lines | cut -d, -f2 -s mixed.csv |
Ignores comment or unseparated lines |
cut -d: -f1 /etc/passwd | head -n 4root
daemon
bin
sys
cut -d: -f1,6,7 /etc/passwd | head -n 2root:/root:/bin/bash
daemon:/usr/sbin:/usr/sbin/nologin
Transforming a colon-delimited /etc/passwd line into a space-separated format:
cut -d: -f1,3,6 --output-delimiter=" | " /etc/passwd | head -n 3root | 0 | /root
daemon | 1 | /usr/sbin
bin | 2 | /bin
Removing the middle column (Field 2) from a 3-column CSV:
echo "Alice,SecretData,Engineer" | cut -d, --complement -f2Alice,Engineer
Extracting fixed-width columns (characters 1–10 and 20–30):
cut -c 1-10,20-30 fixed_records.txtBy default, lines that do not contain the delimiter are printed unmodified. In scripts processing CSV headers or mixed logs, this causes dirty lines. Passing -s suppresses non-matching lines:
cut -d, -f2 -s data_with_comments.csv- Lines lacking a comma are discarded.
| Exit Code | Meaning |
|---|---|
0 |
Success: fields extracted cleanly. |
>0 |
An error occurred (multiple selection modes specified, invalid field range, file unreadable). |
Note
cut strictly accepts a single byte/character as its delimiter (-d). It cannot split streams using multi-character patterns (e.g., ::) or variable-length whitespace (\s+). For multi-character delimiters or irregular whitespace columns, use awk -F"::" or awk '{print $1}'.
- Use
cutfor Simple Delimited Slicing Overawk:- Guidance: For single-character delimited field extraction (
cut -d: -f1), prefercut. - Authoritative Justification: GNU documentation notes
cutis a lightweight C binary with minimal memory footprint and faster throughput than AWK interpreters.
- Guidance: For single-character delimited field extraction (
- Always Use
-son Delimited Log Files:- Guidance: Supply
-swhen processing CSV files that might contain comments or blank lines. - Authoritative Justification: Prevents non-delimited lines from passing through into column pipelines.
- Guidance: Supply
- Use
--complementfor Inverted Slices:- Guidance: Strip unwanted fields using
--complementrather than listing all remaining fields. - Authoritative Justification: Reduces brittle hardcoded column indexes in scripts.
- Guidance: Strip unwanted fields using
- GNU Coreutils cut Manual: https://www.gnu.org/software/coreutils/manual/html_node/cut-invocation.html
- POSIX.1-2024 cut Specification: The Open Group Base Specifications Issue 8. https://pubs.opengroup.org/onlinepubs/9799919799/utilities/cut.html