Where to start First steps track · 15 stages
Linux Labs

cut — cut fields out of lines

5 min Ubuntu 24.04 · GNU coreutils 9.4

The cut command takes fields out of every line by a delimiter (-d and -f) or characters by position (-c).

Cheat sheet

7 commands Printable version
  • cut -d: -f1 /etc/passwd first field, ":" delimiter
  • cut -d: -f1,7 /etc/passwd fields 1 and 7
  • cut -d, -f2- data.csv from field 2 to the end
  • cut -d' ' -f1 access.log the first word of each line
  • cut -c1-8 file.txt positions 1 through 8
  • cut -f2 table.tsv field 2, TAB delimiter
  • cut -d: -f1,6 --output-delimiter=' ' /etc/passwd

Overview

cut splits each line into fields at a single delimiter character and prints the fields listed in -f. -d sets the delimiter; without it, the delimiter is a tab. Fields are counted from one:

text
student@ubuntu:~$ head -n 2 /etc/passwd | cut -d: -f1,7
root:/bin/bash
daemon:/usr/sbin/nologin

cut joins the selected fields with the same delimiter. Numbers can be a list (1,3) or a range (2-4, 3- up to the end of the line). Instead of fields you can cut by position: -c for characters, -b for bytes. On Ubuntu -c counts bytes too: no difference for Latin text, but Cyrillic letters get cut in half.

cut knows only a single delimiter. Two spaces in a row mean an empty field between them. Text aligned with spaces (output of ps, df, uniq -c) is split with awk '{print $2}' — see sed and awk.

Options

7

-f, -c and -b are three ways to pick part of a line; a single call needs exactly one of them. The full list is in man cut.

Option What it does Example
-d --delimiter C Field delimiter — a single character; tab by default. cut -d: -f1 /etc/passwd
-f --fields LIST Which fields to print — 1,3 or 2-4 or 3-. cut -d, -f2- data.csv
-c --characters LIST Cut by position; on Ubuntu it counts bytes, like -b. cut -c1-8 file.txt
-s --only-delimited Skip lines that contain no delimiter. cut -s -d= -f2 settings.conf
--complement Print everything except the selected fields. cut -d: --complement -f2 /etc/passwd
--output-delimiter STR String to join output fields with instead of the input delimiter. cut -d: -f1,7 --output-delimiter=' ' /etc/passwd
-b --bytes LIST Cut by byte positions. cut -b1-4 data.bin

Nothing found. Try a shorter query.

Recipes

5

User names and their shells

bash
cut -d: -f1 /etc/passwd             # every account
cut -d: -f1,7 /etc/passwd | grep -v nologin

Addresses from a web server log

bash
cut -d' ' -f1 /var/log/nginx/access.log |
  sort | uniq -c | sort -rn | head

The first word of an nginx log line is the client address. sort and uniq then count who came most often.

A column from a CSV file

bash
cut -d, -f3 data.csv | sort -u      # distinct values of field 3
cut -d, -f1,3 --output-delimiter=$'\t' data.csv

If CSV values contain commas inside quotes, cut will not understand them: you need a tool that knows the format.

Everything except one field

bash
cut -d: --complement -f2 /etc/passwd    # without the password field

Skip lines without the delimiter

bash
cut -s -d= -f2 settings.conf     # only key=value lines

Without -s, a line with no delimiter is printed whole.

Troubleshooting

7

Empty output with a space delimiter

There is more than one space between words, so the field you asked for is empty. Squeeze the spaces with tr -s ' ' before cut, or take the field with awk '{print $2}'.

cut prints the whole line

The line has no -d delimiter, and cut prints it unchanged. Check the delimiter (: or ,, tab or space), or add -s to skip such lines.

cut: you must specify a list of bytes, characters, or fields

You did not say what to cut. Add -f with field numbers or -c with positions.

cut: the delimiter must be a single character

-d got more than one character. cut takes a single one; for a multi-character delimiter use awk -F.

cut: fields are numbered from 1

Fields are numbered from one: the first field is -f1, not -f0.

cut: only one type of list may be specified

The command has both -f and -c. Keep one of them.

Cyrillic letters are cut in half

On Ubuntu cut -c counts bytes, like -b, and a Cyrillic letter takes two bytes in UTF-8. For non-Latin text, cut by fields (-d, -f) or use awk.

See also

  • sort — order the column you cut out.
  • uniq — count the repeats in a column.
  • wc — how many lines you got.
  • sed-awk — awk splits on any run of spaces and can count.
  • redirection — how to build a pipeline.
  • text-tools — tr, paste, column and other pipeline links.
Loading…