Cheatsheet & Examples: cut
The cut command is used to extract specific parts of a file, such as columns, bytes, or characters, based on provided criteria.
Extract specific fields by default delimiter (tab)
Example Usage:
cut -f1,3 file.txt
What it does:
Extracts the first and third fields (columns) from each line of file.txt, using tab as the delimiter.
Command-line Arguments Explained:
-f1,3: Specifies the fields to extract (1st and 3rd) separated by commas.file.txt: The input file from which to extract the fields.
Extract specific fields by custom delimiter
Example Usage:
cut -d: -f2 file.txt
What it does:
Extracts the second field from each line of file.txt, using a colon (:) as the delimiter.
Command-line Arguments Explained:
-d:: Sets the delimiter to a colon (:) instead of the default tab.-f2: Specifies the field (second) to extract.file.txt: The input file.
Extract specific bytes
Example Usage:
cut -b3-5 file.txt
What it does:
Extracts bytes from positions 3 to 5 (inclusive) of each line in file.txt.
Command-line Arguments Explained:
-b3-5: Defines the byte range to extract (3 to 5).file.txt: The input file.
Extract specific characters
Example Usage:
cut -c1-2,5 file.txt
What it does:
Extracts characters from positions 1 to 2 and position 5 of each line in file.txt.
Command-line Arguments Explained:
-c1-2,5: Defines character ranges to extract (1-2 and 5).file.txt: The input file.
Exclude specific fields
Example Usage:
cut -f1 --complement -d: file.txt
What it does:
Extracts all fields except the first from each line of file.txt, using a colon (:) as the delimiter.
Command-line Arguments Explained:
-f1: Specifies the field to exclude (first).--complement: Tellscutto exclude the specified field instead of including it.-d:: Sets the delimiter to a colon (:).file.txt: The input file.
Suppress lines without the delimiter
Example Usage:
cut -d: -f2 -s file.txt
What it does:
Extracts the second field from each line of file.txt but skips lines that do not contain the colon (:) delimiter.
Command-line Arguments Explained:
-d:: Sets the delimiter to a colon (:).-f2: Specifies the field (second) to extract.-s: Skips lines that do not match the delimiter.file.txt: The input file.
Output with custom delimiter
Example Usage:
cut -d: -f1,3 --output-delimiter=, file.txt
What it does:
Extracts the first and third fields from file.txt using a colon (:) as the delimiter and outputs them separated by a comma (,).
Command-line Arguments Explained:
-d:: Sets the delimiter to a colon (:).-f1,3: Specifies the fields (1st and 3rd) to extract.--output-delimiter=,: Changes the output delimiter to a comma (,).file.txt: The input file.
Extract fields from standard input
Example Usage:
cat file.txt | cut -f1,4
What it does:
Pipes the contents of file.txt into cut and extracts the first and fourth fields from each line.
Command-line Arguments Explained:
cat file.txt: Passes the file's contents tocutvia a pipe.-f1,4: Specifies the fields (1st and 4th) to extract.|: Pipes the output ofcattocut.
Extract a range of fields
Example Usage:
cut -f1-3 file.txt
What it does:
Extracts the first, second, and third fields from each line of file.txt using the default tab delimiter.
Command-line Arguments Explained:
-f1-3: Defines a field range (1st to 3rd).file.txt: The input file.
Extract bytes with offset
Example Usage:
cut -b5- file.txt
What it does:
Extracts bytes starting from position 5 to the end of each line in file.txt.
Command-line Arguments Explained:
-b5-: Defines a byte range starting at position 5 and continuing to the end.file.txt: The input file.
Extract characters with offset
Example Usage:
cut -c5- file.txt
What it does:
Extracts characters starting from position 5 to the end of each line in file.txt.
Command-line Arguments Explained:
-c5-: Defines a character range starting at position 5 and continuing to the end.file.txt: The input file.

