cut command is used to cut out a seleted portions of each line from a file or standard output based on some given conditions and writes them to standard output. In this post I am going to explore cut command with examples.

Synopsis

cut -b list [-n] [file ...]
cut -c list [file ...]
cut -f list [-d delim] [-s] [file ...]

Flags description

-b: cut using byte position
-c: cut using char position
-f: selet fields after using cut with delimeters
-d: cut with delimeter
--output-delimiter=STRING, use STRING as the output delimiter the default is to use the input delimiter
--complement: complement the set of selected bytes, characters or fields

Note: Indexing starts from 1 and --output-delimiter and --complement are not available for BSD version OS.

Examples

Consider the text file rk.txt as input with the following content

Rahul,Kumar,20
Aman,Kumar,21

1. Cut bytes in a range

To cut bytes in a range, pass a - separated index values with -b flag.

$cut -b 1-3 rk.txt
output:
Rah
Ama
$cut -b 2-4,6-8 rk.txt
output:
ahu,Ku
manKum

2. Cut bytes at specific positions

To cut bytes at specific positions, pass a , separated index values with -b flag.

$cut -b 1,3,5 rk.txt
output:
Rhl
Aa,
$cut -b 2,4 rk.txt
output:
au
mn

3. Cut bytes at specific positions and range

$cut -b 2-4,6,7-8,10 rk.txt
output:
ahu,Kua
manKumr

4. Cut char at specific positions and in a range

Use -c command to cut using char index. this is especially useful in cases where the characters are of multiple bytes.

$cut -c 1-3 rk.txt
output:
Rah
Ama
$cut -c 2-4,6,7-8,10 rk.txt
output:
ahu,Kua
manKumr

By looking above examples the output with -c and -b flags are same. Now lets see the output of these two flags with multi bytes char.

$echo "rahul๐Ÿ˜" | cut -b 5,6
output:
l?
echo "rahul๐Ÿ˜" | cut -c 5,6
output:
l๐Ÿ˜

In the above examples ๐Ÿ˜ is a multi byte char hence the output with -c and -b are different.

5. Using delimiter to cut

Using delimiter to cut creates an array of tokens which can be printed with -f flag by giving the field number as argument. following examples uses , as delimiter.

$cut -d "," -f 1 rk.txt
output:
Rahul
Aman
$cut -d "," -f 2 rk.txt
output:
Kumar
Kumar
$cut -d "," -f 2-3 rk.txt
output:
Kumar,20
Kumar,21

6. Changing the output delimiter

$cut -d "," -f 1,2,3 --output-delimiter ":" rk.txt
output:
Rahul:Kumar:20
Aman:Kumar:21
$cut -c 1,2  --output-delimiter ":" rk.txt
output:
R:a
A:m
$cut -b 1,2  --output-delimiter ":" rk.txt
output:
R:a
A:m

7. Inverting the output of cut command

$cut --complement -d ',' -f 1 rk.txt
output:
Kumar,20
Kumar,21
$cut --complement -c 1,2 rk.txt
output:
hul,Kumar,20
an,Kumar,21
$cut --complement -b 1,2 rk.txt
output:
hul,Kumar,20
an,Kumar,21