Linux Basics: Selecting Text Fields with cut
When each line contains fields separated by the same character, cut can extract just the columns you need. It is useful for simple lists and tab-separated data on Debian and Ubuntu.
Run mktemp -d, then use cd to enter the temporary directory it prints. The examples below create their own practice file.
Create a predictable format
printf 'apple:fruit:red\ncarrot:vegetable:orange\n' > items.txtEach record has three colon-separated fields: name, group, and color. Choose a delimiter that does not also appear inside your field values.
Select one field or a range
cut -d ':' -f 1 items.txt
cut -d ':' -f 2-3 items.txt-d sets the delimiter. -f selects field numbers, starting at one. A range such as 2-3 includes both endpoints and preserves the delimiter between the selected fields.
Without -d, the field delimiter is a tab. Repeated spaces are not automatically treated as one separator, so visually aligned terminal output may need a different tool.
Select separated fields
cut -d ':' -f 1,3 items.txt
cut -d ':' -f 1,3 --output-delimiter=' | ' items.txtapple:red
carrot:orange
apple | red
carrot | orangeA comma-separated list selects multiple fields. GNU cut writes them in their original order, even if you request the field numbers in a different order. The last example changes only the separator.
By default, a line without the delimiter is printed unchanged. Add -s to skip those lines when extracting fields.
cut does not understand CSV quoting. Use a CSV-aware parser when a quoted field can contain commas or newlines.
Reference
Acknowledgements
This post was created with assistance from AI.
Linux Basics — lesson 9 of 25. Series index
Previous: Sorting and Counting Text Lines · Next: Comparing Files with diff and cmp