Start with one column
An inventory export arrives with a hostname, role, owner, and state on each line. You need the owners, not another program that understands the whole inventory. cut is useful when the layout already has a dependable contract: it selects positions and leaves the selected content alone. The interesting question is how to recognize when that contract has stopped being dependable.
This article uses GNU Coreutils 9.4 cut on Ubuntu with Bash 5.2.21. Field selection is widely available, but the GNU extensions below and the observed character behavior need their own qualifications. Start with a UTF-8 file called owners.txt containing exactly these two lines: edge01|web|maya|ready and edge02|db|leo|ready. Each line ends with a newline.
The -d option sets the field delimiter to the vertical bar. Single quotes keep the bar from becoming a shell pipe operator. The -f option selects field 3, counting from 1. The filename supplies the input; cut prints the result to standard output, normally your terminal. It does not edit the file. Its model is per-record projection: keep selected portions of each input record, in their original order.
cut -d '|' -f 3 owners.txtmaya
leo
Without -d, cut uses a tab as its field delimiter. A pipe-delimited line then has no tab, which invokes a special passthrough rule we will examine shortly.
Make a deliberately untidy inventory
To test that model, make a small export with ordinary records and several awkward ones. Work in an empty disposable directory, and keep owners.txt from the opening example beside it. The command below creates inventory.txt; if that filename already exists, it overwrites it. No subsequent cut example modifies either file.
cat copies its input to its output. The shell's > redirects that output into inventory.txt. The <<'DATA' construction is a quoted here-document: the following lines become literal input until the closing DATA line. Quoting the marker prevents variable expansion inside this fixture. The closing marker must stand alone.
Read the anomalies before running anything. edge02 has an empty owner; edge03 has no state field; the collector diagnostic contains no delimiter at all; edge04 has an extra field. They represent different failures, and a projection can make some of them disappear. There is no schema awareness hiding behind the short command.
cat > inventory.txt <<'DATA'
edge01|web|maya|ready
edge02|db||ready
edge03|worker|leo
collector unavailable
edge04|web|sam|ready|extra
DATAThe setup prints nothing. Verify the file visually if you entered it by hand; the examples depend on these exact separators and line endings.
A selection list cannot rearrange fields
The inventory's state belongs in field 4, but perhaps a report needs state followed by hostname. Writing the desired field numbers in that order looks plausible. Run this selection to see why it does not produce that report.
The comma-separated argument 4,1,4 is a set of selected positions. GNU cut emits those positions in input order, once each. Repetition does not duplicate a column, and order in the argument does not reorder output. edge03 contributes only its hostname because it lacks field 4. The diagnostic passes through unchanged.
A range such as -f 2-3 selects fields 2 through 3, inclusive; -f 3- selects field 3 through the last field. Ranges describe positions, not a transformation language. Overlapping ranges likewise do not duplicate selected input.
For actual reordering, the earlier awk article supplies the right model: evaluate a program for each record. On owners.txt, awk -F '[|]' '{print $4, $1}' produces ready edge01 and ready edge02. -F sets its field-separator pattern; [|] means one literal bar in that pattern. The quoted braces contain the awk program, $4 and $1 refer to fields, and print separates its comma-separated arguments with a space. The shell quotes prevent it from expanding the dollar signs.
cut -d '|' -f 4,1,4 inventory.txtedge01|ready
edge02|ready
edge03
collector unavailable
edge04|ready
Suppressing diagnostics does not validate records
The collector line in an owner report is an unwelcome surprise. With field selection, a line containing no delimiter is normally copied verbatim, even if the requested field number is far beyond it. The -s flag suppresses those delimiter-free lines.
Now select owner field 3. The result contains four output records, including an empty second line. Adjacent delimiters create an empty field; they do not collapse into one separator. edge03 still has its owner, even though its state is missing. The extra field on edge04 does not prevent extraction either.
Selecting field 4 instead yields ready, ready, an empty line, and ready. A missing selected field and an existing-but-empty field can both become an empty output record. -s only checks whether a delimiter occurs, not how many fields exist, whether required values are populated, or whether values are valid. A successful exit status means cut completed its operation, not that the input passed an inventory audit.
This matters downstream: a consumer that discards blank lines can silently change the correspondence between rows and owners. Retain hostnames beside selected values when you need to investigate an anomaly.
cut -s -d '|' -f 3 inventory.txtmaya
leo
sam
Remove a column, retain everything else
When an export grows more columns, listing every field except the owner becomes tedious. GNU --complement reverses the selection: -f 3 identifies the field to omit rather than the field to retain. Here it keeps all existing fields other than the third.
GNU --output-delimiter supplies the separator used between retained fields. The quoted argument ' / ' contains a space, a slash, and another space; it is one argument, not three. This separator can be a string even though our input delimiter is one byte. Without this option, the retained fields would still be joined by a bar.
The extra value on edge04 remains. That is convenient for an extensible report and dangerous for a supposed fixed-schema export. Removing a column by position removes exactly that position, not everything semantically associated with it. Changing the output separator also does not quote or escape embedded separators. If an owner or another field can contain ' / ', the resulting display is ambiguous.
The installed GNU manual documents these operations. Treat them as GNU extensions rather than assuming an identical command on every Unix machine.
cut -s -d '|' --complement -f 3 --output-delimiter=' / ' inventory.txtedge01 / web / ready
edge02 / db / ready
edge03 / worker
edge04 / web / ready / extra
A space is a separator, not whitespace grammar
A display that looks columnar need not have a field contract. Suppose a command prints edge01, two spaces, web, three spaces, and maya. Selecting the second space-delimited field gives an empty record. Each separator counts independently: the first field ends at the first space, and the second field lies between the first and second spaces.
printf supplies this tiny input without another file. Its '%s\n' format prints the following string and a newline; single quotes preserve both the format and the spaces. The pipe sends that output into cut, which reads standard input because no filename was supplied.
For whitespace-separated words, awk's default splitting ignores leading and trailing whitespace and treats runs of spaces or tabs as separators. The command printf '%s\n' 'edge01 web maya' | awk '{print $2}' prints web. The short program asks for the second field, using the field notation introduced above.
That still does not make a screen display a reliable interface. A field containing spaces, a wrapped line, or a header can defeat the assumption. Prefer a producer's documented machine-readable output over repairing its human-readable alignment. GNU9.4 rejects -d '||' as an invalid multicharacter delimiter; supplying two bars does not request a two-byte separator.
printf '%s\n' 'edge01 web maya' | cut -d ' ' -f 2
Quotes in the file are just data
Comma-separated data provides another tempting shortcut. In the supplied record below, the role is web,public enclosed in double quotes. A CSV reader would recognize those quotes as part of the format and return maya as the third logical field. cut has no CSV quote state. It splits at every comma.
The outer single quotes are shell syntax: they preserve the entire input string, including its double quotes. Those inner double quotes reach cut as ordinary input bytes. Field 3 is therefore public followed by a quote. Nothing about a zero exit status reveals the mistake.
Use a CSV parser for a format with quoted delimiters, escaped quotes, or embedded newlines. Ordinary awk field splitting is not a general CSV parser either. It becomes appropriate when you genuinely have simple delimiter-separated fields or deliberately implement and verify the required grammar.
An occasional correct result on a simple row is weak evidence. Test the producer's permitted awkward values, not merely the values your current inventory happens to contain.
printf '%s\n' 'edge01,"web,public",maya' | cut -d ',' -f 3public"
Byte positions work when the contract is bytes
Sometimes the contract is fixed width rather than delimited: six ASCII bytes of hostname, followed immediately by state. The -b option selects byte positions, also counting from 1. The range 1-6 extracts the prefix of each record.
ASCII characters in this fixture occupy one byte each, so the six-byte prefix also looks like six characters. Tabs still occupy one byte despite their variable display width. Terminal columns, screen alignment, and bytes are different measurements.
This is useful for a documented fixed-width text export. Selection restarts on each newline-delimited record; it does not extract offsets from an entire arbitrary binary file. GNU9.4 also emits a newline for the final output record when the input lacks one. Selecting bytes 1-3 from ABCDEF with no final newline produces ABC followed by a newline. That terminator is added output, not a selected input byte.
Short records produce whatever selected bytes exist; they are not automatically rejected for being short. Changing a hostname from ASCII to another encoding can invalidate an apparently stable width. Ranges under -b obey the original-order, once-only selection model within each record. GNU --output-delimiter can insert a string between separated selected byte ranges within that record, adding further output bytes. These are text-record operations, not a promise of an unchanged evidence slice.
printf '%s\n' 'edge01ready' 'edge02failed' | cut -b 1-6edge01
edge02
Check what character selection really means
The -c option is named character selection, which sounds like the cure for multibyte text. On the tested GNU9.4 implementation, it behaves like -b. The installed official manual explicitly describes that behavior, and the following UTF-8 test reproduces it. This is a version-specific finding, not a prediction about a newer implementation.
The supplied é consists of bytes c3 a9 in UTF-8; X is 58. LC_ALL=C.utf8 sets the locale for cut in this pipeline. The shell applies that assignment only to that command. The locale is available in the test environment; on another system, use an installed UTF-8 locale.
od displays bytes rather than asking the terminal to render damaged text. -An suppresses address offsets, and -t x1 requests one-byte hexadecimal units. The output contains c3 followed by newline byte 0a: cut retained only the first half of é. The pipeline's last newline is a record terminator, not part of the selected character.
GNU9.4 also documents -n as a no-op. Replacing -c 1 with -b 1 -n in this example produces the same bytes. Do not rely on either spelling here for Unicode-safe truncation. Code points, user-perceived characters, and display columns also differ; choose an encoding-aware tool appropriate to the measurement you actually need.
printf 'éX\n' | LC_ALL=C.utf8 cut -c 1 | od -An -t x1 c3 0a
Preserve records containing newlines
Field boundaries and record boundaries are separate decisions. A filename can contain a newline, so newline-delimited pathname output can become ambiguous. The find article explains zero-byte-terminated output, and xargs explains consuming it without reparsing filenames.
GNU -z changes cut's record separator from newline to NUL, a zero byte. It also terminates output records with NUL. In this controlled demonstration, two synthetic paths begin with root/. The first contains a newline inside alpha followed by part. No files are created or accessed.
printf interprets \n as newline and \000 as a zero byte in its format. cut selects slash-separated fields 2 through the end, removing the known root component. od lets us see the surviving newline byte 0a inside the first record and the two record-ending 00 bytes.
Keep a NUL-aware consumer throughout such a pipeline. A normal line-oriented filter would reinterpret the embedded newline as a boundary. The ordinary slash still separates path components; -z does not validate path structure, authorize access, normalize parent-directory components, or make a transformed path safe to execute. This example proves record transport for a supplied format, not a security property of arbitrary paths.
printf 'root/alpha\npart/report\000root/beta/report\000' | cut -z -d / -f 2- | od -An -t x1 61 6c 70 68 61 0a 70 61 72 74 2f 72 65 70 6f 72
74 00 62 65 74 61 2f 72 65 70 6f 72 74 00
Keep the context that makes evidence useful
A supplied permissions export might contain service|0600|config/edge01 and service|0644|config/edge02, with a newline after each record. Save those exact lines as permissions.txt. The numeric modes are source data here; cut neither reads filesystem permissions nor changes them.
Selecting account and pathname makes a compact list, but it removes the very difference that could matter in an access review. Both records below look equally routine even though the retained original contained different modes. Investigations need the original artifact and its provenance as well as any extracted view.
Similarly, --complement can omit a known credential column from a controlled fixture, but it is not a secret detector or sanitization engine. Additional fields, misplaced delimiters, multiline values, or delimiter-free diagnostics can leak unexpected material. -s reduces one passthrough case; it does not establish that remaining records contain only approved data.
Projection is useful for narrowing an artifact before comparing inventories or inspecting account exposure. Keep the deciding fields alongside identifiers, validate the documented schema separately, and review what is being released. Do not feed the resulting strings into shell evaluation merely because a tool selected them.
cut -s -d '|' -f 1,3 permissions.txtservice|config/edge01
service|config/edge02
Choose the next stage deliberately
Return to the inventory and select hostname plus state. The empty owner is irrelevant to that projection, while edge03's missing state is still visible as a shortened output record. edge04's additional field disappears from the view. This is a useful compact report only if you understand what it preserves and what it conceals.
For a stable delimiter and existing numbered fields, cut makes its intent easy to inspect. Use awk when the decision depends on field contents, counts, arithmetic, or reordering; keep the local parsing rules explicit. Use a proper format parser when quotes or escapes change delimiter meaning. Byte slicing belongs to a byte contract, not a visual guess.
The productive habit is to identify the contract first: what ends a record, what separates fields, whether empty fields count, and whether width means bytes or characters. Then a short extraction command is something you can justify rather than something that merely worked on the first row.
cut -s -d '|' -f 1,4 inventory.txtedge01|ready
edge02|ready
edge03
edge04|ready