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

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

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

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

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

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

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

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

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

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

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

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

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

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

Display the first four characters from each line of text.

Print the characters from thirteenth position to the end.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Display the version of the cut command.

Last updated