Filter Text with the Linux cut Command
Learn how to use Linux cut to extract characters, character ranges, and delimiter-separated fields from files and command output.
The Linux cut command extracts selected portions of every input line. It can select byte positions, character positions, or delimiter-separated fields. Unlike grep, which selects lines matching a pattern, cut keeps the lines and removes the portions you did not request.
This makes cut useful for simple text filtering: processing text to retain or extract useful portions of input. Input can come from a named file or from standard input, such as the output of another command in a pipeline.
This lesson assumes familiarity with shell commands, files, basic options, quoting, pipes, and standard input. For background, see Linux command-line fundamentals and Bourne Again Shell (Bash).
Basic cut Command Structure
The general form is:
cut OPTION... [FILE...]
A selection option is required. The most common choices are -c for character positions and -f for fields. The optional file names identify the input. If no file is specified, cut reads standard input.
cut -c 3 results.txt
printf 'red,green,blue\n' | cut -d ',' -f 2
The first command reads results.txt. The second command sends the output of printf through a pipeline to cut.
Common cut Selection Options
| Option | Purpose | Example | Result description |
|---|---|---|---|
-c N | Select one character position | cut -c 3 results.txt | Prints character 3 from every line |
-c START-END | Select a closed character range | cut -c 2-6 results.txt | Prints characters 2 through 6 |
-c -END | Select from the beginning through a position | cut -c -5 results.txt | Prints characters 1 through 5 |
-c START- | Select from a position through the end | cut -c 8- results.txt | Prints character 8 and all later characters |
-f N | Select one field | cut -f 2 results.txt | Prints field 2 using tabs by default |
-f LIST | Select fields or field ranges | cut -f 1,3 results.txt | Prints fields 1 and 3 |
-d DELIMITER | Change the field separator | cut -d ':' -f 1 /etc/passwd | Uses a colon instead of a tab |
Selecting Characters with -c
The -c option selects character positions. A character position is a one-based location within a line: the first character is position 1, the second is position 2, and so on. Positions are counted separately for each input line.
Select One Character
To print the third character of every line in results.txt:
cut -c 3 results.txt
If the file contains alpha, bravo, and charlie, the output is p, a, and a, respectively. The command extracts position 3; it does not search for the character or select only lines containing it.
Select a Character Range
A character range is a contiguous group of positions. Use START-END to include both endpoints:
cut -c 2-6 results.txt
This prints characters 2, 3, 4, 5, and 6 from every line.
Select from the Beginning or Through the End
Use -END to select from position 1 through the specified position:
cut -c -5 results.txt
Use START- to select from a position through the end of each line:
cut -c 8- results.txt
You can also list individual positions and ranges together, such as -c 1,3,5-7. Positions are still counted from 1.
Selecting Fields with -f
A field is a section of a line separated from other sections by a delimiter. A delimiter is a character such as a tab, slash, colon, comma, or space that separates fields.
The -f option selects fields, and field numbering also starts at 1:
cut -f 1,3 results.txt
This command prints the first and third fields from each line. It assumes that the input is tab-delimited because a tab character is the default delimiter for field mode.
You can select a single field, several fields, or a range:
cut -f 2 results.txt
cut -f 1,3,5 results.txt
cut -f 2-4 results.txt
cut -f 1- results.txt
The notation follows the same general pattern as character ranges: 2-4 means fields 2 through 4, while 1- means field 1 through the final field.
Characters Versus Fields
| Selection mode | What is selected | Typical option | Best use case | Example input format |
|---|---|---|---|---|
| Characters | Positions within each line | -c | Fixed-width or consistently positioned text | AB-12345 |
| Fields | Delimiter-separated sections | -f, often with -d | Structured records with a known separator | alice:1001:/home/alice |
Changing the Delimiter with -d
Use -d followed by one delimiter character when fields are not separated by tabs. The delimiter is normally quoted when quoting makes the command clearer or when the character has shell meaning.
Slash-Separated Text
To print the third field from slash-separated lines:
cut -d '/' -f 3 results.txt
For an input line such as usr/local/bin, the fields are usr, local, and bin. The command prints bin.
Colon-Separated System Data
The local account database at /etc/passwd uses colons to separate fields. To extract usernames:
cut -d ':' -f 1 /etc/passwd
This selects field 1 from each colon-delimited record. The file may contain system accounts as well as regular user accounts, so the output is not necessarily a list of only human users.
Comma- and Space-Separated Values
For comma-separated text, use a comma as the delimiter:
printf 'red,green,blue\n' | cut -d ',' -f 2
The result is green. A literal space can be supplied as the delimiter, commonly with quotes:
cut -d ' ' -f 2 data.txt
However, this treats each individual space as a separator. It is not a reliable way to process output where columns are aligned with varying numbers of spaces.
Using cut in Pipelines
A pipeline uses the pipe operator, |, to send one command's standard output to another command's standard input. This is useful when a command already produces consistently delimited data.
printf 'red,green,blue\n' | cut -d ',' -f 2
Here, printf produces one comma-separated line, and cut extracts its second field.
When building a pipeline, first identify a stable delimiter. If the producer emits tabs, use the default field mode or an explicit tab-aware approach. If it emits colons or commas, specify that character with -d before selecting fields.
some_command | cut -d ':' -f 1,3
This pattern is appropriate only when some_command consistently produces colon-separated records. For system text, a direct example is:
cat /etc/passwd | cut -d ':' -f 1
The direct file form is simpler in this case:
cut -d ':' -f 1 /etc/passwd
Lines Without the Selected Delimiter
In field mode, a line that does not contain the selected delimiter may be passed through as a whole line. For example, if you run cut -d ':' -f 2 and one input line has no colon, that line can appear unchanged rather than as an empty field.
If your implementation supports it, -s suppresses lines that do not contain the delimiter:
cut -s -d ':' -f 2 data.txt
Use this only when dropping malformed or non-delimited lines is the desired behavior. Otherwise, validate the input format before extracting fields.
Limitations and Choosing Another Tool
cut works best when character positions are consistent or fields have a consistent, known delimiter. It is intentionally simple and does not parse arbitrary text layouts.
- Variable spaces: Several spaces between columns are separate delimiter characters to
cut. Useawkfor whitespace-separated columns when spacing varies. - Pattern matching: Use
grepwhen the goal is to select lines based on text patterns. - Substitution and stream editing: Use
sedfor substitutions, deletions, and other line transformations. - Flexible field processing: Use
awkfor calculations, conditions, variable whitespace, and computed output. - Combining columns: Use
pasteto join corresponding lines or columns from files. - Ordering and deduplication: Use
sortanduniqwhen records must be ordered or repeated values removed. - Column display: Use
columnwhen the goal is formatting data into readable aligned columns. - Complex CSV: A comma is not always a safe CSV delimiter because quoted values can contain commas. Use a CSV-aware tool for quoted CSV data.
Troubleshooting cut
The Expected Field Is Not Returned
The input may not use the default tab delimiter. Inspect the data and specify its actual separator:
cut -d ',' -f 2 data.txt
A Line Without a Delimiter Returns the Whole Line
This is normal field-mode behavior on many implementations. Check the input format, or use -s when lines without the delimiter should be suppressed.
Space-Aligned Output Produces Unexpected Fields
Several spaces may occur between displayed columns. Since cut -d ' ' treats each space separately, field numbers will not correspond reliably to visual columns. Prefer a producer with a deliberate delimiter or use awk.
Accented or Non-ASCII Characters Look Wrong
Character and byte handling can depend on the installed implementation and the current locale. Check the local cut documentation and locale settings. For demanding Unicode processing, choose a Unicode-aware tool.
cut Reports That a List Option Is Required
Add a selector such as -c 1-5, -f 2, or the appropriate byte-selection option supported by your implementation. Running cut without a character, byte, or field selection does not specify what to extract.
Exam-Relevant Notes
cutextracts portions from every input line; it does not search for matching lines.- Character positions and field numbers start at 1, not 0.
-cselects character positions;-fselects fields.- Field mode uses a tab character by default.
- Use
-dto specify a different single-character delimiter. START-ENDis inclusive,-ENDstarts at position 1, andSTART-continues through the end.- Always verify the delimiter before using
-f. - Use
awkrather thancutfor variable whitespace or more complex field logic.
Summary
cut is a focused Linux text-filtering command for extracting fixed character positions or delimiter-separated fields. Use -c for character ranges, -f for fields, and -d when the delimiter is not a tab. It can read files directly or receive standard input through a pipeline. Its reliability depends on consistent input structure, so choose a more flexible tool when spacing, quoting, or field rules vary.