Back to course

57. Extracting Data: The `cut` Command

Linux Basics: From Zero to CLI Hero

Columnar Data Extraction

The cut command is designed to extract specific sections (columns or fields) from each line of input based on delimiters.

Syntax

cut [options] [file...]

Using Delimiters (-d)

Use the -d option to specify the delimiter (the character separating the fields).

Extracting Fields (-f)

Use the -f option to specify which field numbers to extract.

Example: Extracting usernames and shells from /etc/passwd

/etc/passwd uses the colon (:) as a delimiter. Usernames are Field 1, and shells are Field 7.

bash $ cut -d ':' -f 1,7 /etc/passwd root:/bin/bash daemon:/usr/sbin/nologin ...

Extracting Character Ranges (-c)

You can also extract specific character positions regardless of delimiters.

bash

Get the first 10 characters of a file

$ cut -c 1-10 data.txt