JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Linux extraction command cut

Author:JIYIK Last Updated:2025/04/08 Views:

The cut command belongs to the pipeline command family. Cut is an extraction command that extracts the data we want after analyzing a piece of data. Generally, information is extracted line by line. The meaning of cut is to cut. Yes, the purpose of this command is to cut out a piece of information. The information it processes is in lines. Let's take a look at the usage of the cut command.

# cut [options]…

We will explain the options of the cut command in groups.

Option 1

-d: followed by a separator character. Used to determine which character to use to separate a line of data.
-f: Split a piece of information into several segments based on the separator of -d. Use this option to specify which segment to extract.

Note: 1. –d and –f must be used together, otherwise the command will not be executed correctly;

2. The characters following –f can be divided into several situations

N selects the Nth segment of data to output, and starts counting from 1;
N- starts from the Nth segment of data to the last segment;
N,M intercepts the data of the N and M fields
NM starts from the Nth segment of data to the Mth segment;
-M starts from the 1st segment to the Mth segment;

Let's look at an example of how to view the different fields of the /etc/ passwd file.

# cat /etc/passwd | cut –d ':' –f 3,5 //Intercept data from the third and fifth fields
# cat /etc/passwd | cut –d ':' –f 3-5 //Intercept data from the third to the fifth fields
# cat /etc/passwd | cut –d ':' –f 3 //Intercept data from the third field
# cat /etc/passwd | cut –d ':' –f 3- //Intercept data from the third field (including the third field)
# cat /etc/passwd | cut –d ':' –f -5 //Intercept data from the first field to the fifth field

Here we explain that when displaying the intercepted data, the data segments are connected by the character specified by -d by default.

0:root
1:bin

We can specify the character for the connection through the option --output-delimiter

# cat /etc/passwd | cut –d ':' –f 3,5 –output-delimiter=';'
0;root
1;bin

There is another option to note: --only-delimited. This means that only lines with the character specified by -d are displayed. What does it mean? That is, we use -d to specify the character '0' as the delimiter, but some data does not have '0'. By default, data without '0' will also be displayed. If we do not want it to be displayed, we can use --only-delimited.

# cat /etc/passwd | wc –l
31 //The file has 31 lines of data.
# cat /etc/passwd | cut –d '0' –f 3,5 | wc –l
31 //The result also has 31 lines, but we check the content of the file and find that some lines do not have '0'.
# cat /etc/passwd | cut –d '0' –f 3,5 –only-delimited | wc –l
10 //The result is only 10 lines left, and these 10 lines of data all contain the data we want, which is '0'.

Option 2

-c intercepts fixed character intervals in char units
-b intercepts fixed byte intervals in byte units
-n This option is used together with -b to indicate that characters that occupy multiple bytes will not be split. For example, if a Chinese character occupies multiple characters, this Chinese character will not be split into multiple bytes for interception.

Note: 1. -b and -c cannot be used at the same time

2. The number specified after -c and –b is the same as that of -f

See the following example

# cat /etc/passwd | cut –c 12- //Take the string after 12 characters
# cat /etc/passwd | cut –bn 12- //Take all the characters after 12 bytes

The main purpose of cut is to decompose the data in the same row. It is most commonly used when analyzing some data or text data.

That’s all we have to say about the usage of cut. I hope this article will be helpful to you.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Using onclick to execute a PHP function

Publish Date:2025/04/13 Views:155 Category:PHP

We will also introduce another onclick() method of executing PHP functions through events using the jQuery library. This method calls a JavaScript function that will output the content of the PHP function in the web page. We will also demon

Executing SQL Queries in Pandas

Publish Date:2025/04/12 Views:183 Category:Python

SQL stands for Structured Query Language; it is a well-known language for interacting with relational databases. There are many ways to run SQL queries in Python. pandasql Run SQL queries in Python using This package has a method similar to

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Restart PostgreSQL in Ubuntu 18.04

Publish Date:2025/04/09 Views:72 Category:PostgreSQL

This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n

Issues to note when installing Apache on Linux

Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM

As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial