Skip to main content

Module 3: Text Processing & Search

Lesson 29: cut Command

In this lesson, you'll learn how to use the cut command to remove and extract specific sections from lines of text in Linux.

The cut command is one of the text-filtering utilities that is utilized to remove specific sections from input lines. It performs filtering based on byte positions, characters, fields, and delimiters.

cut Command Syntax

The syntax of the cut command is just like any other Linux command:

$ cut <OPTIONS>... [FILE-1] [FILE-2] ...

In the above syntax, the angular bracket <> represents the mandatory arguments, whereas the square bracket [] represents the optional parameters.

cut Command Options

Option Description
-b LIST Select only the bytes at the specified positions
-c LIST Select only the characters at the specified positions
-d DELIM Use DELIM as the field delimiter instead of TAB
-f LIST Select only the specified fields
--complement Print all bytes or characters except the selected ones

Now that we are familiar with the syntax of the cut command, let's create a sample file to use as an example:

$ cat file.txt
Linux is a powerful operating system.
It is widely used in servers and cloud environments.
The command line is the heart of Linux administration.

1. Print First Byte of a File

The cut command allows us to extract text based on byte position using the -b option.

Let's use the command below to extract the first byte from each line of the file:

$ cut -b 1 file.txt
L
I
T

In this example, we can see that the cut command shows only the first character because all characters are one byte long.

2. Print Multiple Bytes of a File

In the previous example, we saw how to select a single byte from the file. However, the cut command also allows us to select multiple bytes using the comma.

Let's use the command below to select the first four bytes from the file:

$ cut -b 1,2,3,4 file.txt
Linu
It i
The

In this example, we have selected the consecutive bytes, but that is not mandatory. We can use any valid byte position with the cut command.

3. Print a Range of Bytes of a File

Pro TecMint Β· Root Plan
This Article is for Root Members
Join Root to read the full article and unlock everything

Full Access to Every Article, Course & Certification Track

Join thousands of Linux professionals who use Pro TecMint to advance their careers.

Ad-free access to all premium articles
All courses: Learn Linux, Bash, Golang, Ubuntu and more
RHCSA, RHCE, LFCS & LFCA certification prep
New courses added every month
Private Telegram community & priority support
Root Plan
$8/mo
or $59/year billed annually
Save $37 with annual plan
Start Reading This Article in the Next 60 Seconds
Join Root Plan β†’