cut

cut linux command cheatsheet by Thamizhiniyan C S

Introduction

The cut command is used to remove sections from each line of files.


Syntax

cut OPTION... [FILE]...


Important Flags

FlagDescription

-b, --bytes=LIST

Selects only the bytes specified in LIST (e.g., -b 1-3,7).

-c, --characters=LIST

Selects only the characters specified in LIST (e.g., -c 1-3,7).

-d, --delimiter=DELIM

Uses DELIM as the field delimiter character instead of the tab character. (default: TAB)

-f, --fields=LIS

Selects only the fields specified in LIST, separated by the delimiter character (default is tab).

-n

Do not split multi-byte characters (no effect unless -b or -c is specified).

-s

do not print lines not containing delimiter

-z

line delimiter is NUL, not newline

–complement

Invert the selection of fields/characters. Print the fields/characters not selected.


Examples

CommandDescription
cut -b 1,2,3 readme.txt

Extract specific bytes: 1, 2, and 3 from each line of the file.

cut -b 1-3,5-7 readme.txt

Extract specific bytes with ranges: 1-3 and 5-7 from each line of the file.

cut -b 1- readme.txt

Extract bytes from the beginning to the end of each line.

cut -b -3 readme.txt

Extract bytes from the beginning up to the 3rd byte of each line.

cut -c 1-7 readme.txt

Extract the first seven characters from each line of the file.

cut -c 2,5,7 readme.txt

Extract specific characters: 2, 5, and 7 from each line of the file.

cut -c 1- readme.txt

Extract characters from the beginning to the end of each line.

cut -c -5 readme.txt

Extract characters from the beginning up to the 5th character of each line.

cut -c 5-10 text.txt

Extract characters 5 to 10 from each line of the file named text.txt.

cut -c 1-5,10-15 data.txt

Extract characters 1 to 5 and 10 to 15 from each line of the file data.txt.

cut -c 3

To print the 3rd character from each line as a new line of output

cut -c 2,7

Display the 2nd and 7th character from each line of text.

cut -c 2-7

Display a range of characters starting at the 2nd position of a string and ending at the 7th position (both positions included)

cut -c -4

Display the first four characters from each line of text.

cut -c 13-

Print the characters from thirteenth position to the end.

cut --complement -d " " -f 1 readme.txt

Extract all fields except the first field using space as the field separator.

cut --complement -c 5 readme.txt

Extract all characters except the 5th character from each line of the file.

cut -d " " -f 1 readme.txt

Extract the first field from each line using space as the field separator.

cut -d " " -f 1-4 readme.txt

Extract fields 1 to 4 from each line using space as the field separator.

cut -d " " -f 1,2 readme.txt --output-delimiter='%'

Extract fields 1 and 2 from each line using space as the field separator, with '%' as output delimiter.

cat readme.txt \| cut -d ' ' -f 1 \| sort -r

Extract the first field from each line and sort the output in reverse order.

cat readme.txt \| head -n 3 \| cut -d ' ' -f 1 > list.txt

Extract the first field from the first 3 lines and redirect the output to list.txt.

cut -d',' -f1,3 data.csv

Extract the first and third columns from a CSV file named data.csv.

cut -d':' -f1,3 data.txt

Extract the first and third fields from the file data.txt using colon as the delimiter.

cut -d',' -f1,3 data.csv > output.txt

Extract the first and third fields from the file data.csv and store the output in output.txt.

cut -d ' ' -f 4

Given a sentence, identify and display its fourth word. Assume that the space (' ') is the only delimiter between words.

cut -d ' ' -f -3

Given a sentence, identify and display its first three words. Assume that the space (' ') is the only delimiter between words.

cut -f 1 readme.txt

Extract the first field from each line of the file using the default tab delimiter.

cut -f -3

Given a tab delimited file with several columns (tsv format) print the first three fields.

cut -f 2-

Given a tab delimited file with several columns (tsv format) print the fields from second fields to last field.

cut --version

Display the version of the cut command.

Last updated