Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Use the Linux cut Command

Pull out columns, characters or bytes from each line of a file or from a command output.

VPS and Cloud Servers2 min read12 screenshots

Syntax

cut [OPTION] [FILE]

OPTION says what to cut and FILE is the input. If you leave out the file, cut reads from a pipe.

Common options

OptionMeaning
-b, --bytesSelect bytes
-c, --charactersSelect characters
-f, --fieldsSelect fields (columns)
-d, --delimiterSet the field separator (the default is a tab)
--complementShow everything except the selection
-s, --only-delimitedSkip lines that have no delimiter
--output-delimiterSet the separator of the output

Examples

These examples use a file called file.txt.

Bytes

cut -b 1-5 file.txt
Extracting bytes 1 to 5

It prints the first 5 bytes of each line.

Characters

cut -c 1 file.txt
Extracting the first character

Use a range for more characters:

cut -c 1-10 file.txt
Extracting characters 1 to 10

Use a comma for several ranges:

cut -c 1-10,20-30 file.txt
Extracting several ranges

Fields with a delimiter

Take the second field of a comma-separated file:

cut -d ',' -f 2 file.txt
Extracting the second field

Take the second and fourth fields:

cut -d "," -f 2,4 file.txt
Extracting several fields

Take a range of fields, with a space as the separator:

cut -d " " -f 2-4 file.txt
Extracting a range of fields

Change the output separator

cut -d ',' -f 1,2 --output-delimiter=';' file.txt
A semicolon as the output delimiter

Skip lines without a delimiter

cut -d "," -s -f 1 file.txt
Suppressing lines without a delimiter

Use cut after another command

List the user names of the system (the first field of /etc/passwd):

getent passwd | cut -d ':' -f1
User names with getent and cut

Show everything except one field

cut -d ',' --complement -f1 file.txt
The complement option

For all options, run:

cut --help
The help page of cut

Quick recap

  • -d sets the separator and -f picks the fields.
  • -c picks characters and -b picks bytes.
  • Pipe other commands into cut to get a clean list.