Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the cut command in Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use the cut command in Linux". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn how to use the cut command in Linux.

Cut commands and syntax

The basic syntax of the cut command is as follows:

$cut OPTION... [FILE]...

Let's take a look at some of the options for cut, and the cut command must be specified before it can be executed.

-f: extracts the specified field, and the cut command uses Tab as the default delimiter.

-d: Tab is the default delimiter, which allows you to specify your own delimiter.

-b: extracts the specified bytes or you can specify a range.

-c: extracts the specified character, either a comma-separated list of numbers or a range of numbers separated by hyphens.

-complement: supplement the selected part, that is, reverse selection.

-output-delimiter: the delimiter used to modify the output.

-- only-delimited: does not output columns that do not contain delimiters.

Let's take the following text file named context.txt and / etc/passwd file as an example to illustrate.

$cat content.txt Ubuntu Linux Microsoft Windows OsX El Capitan Unix FreeBSD

How to specify a delimiter

The most common option is a combination of-d and-f, which extracts content based on the delimiter specified by-d and the fields listed by-f.

For example, in this example, only the first field of each line of the / etc/passwd file is printed, with the delimiter:

$cut-dazzlement'- F1 / etc/passwd root bin daemon adm lp sync shutdown halt mail operator games alvin liangxu...

In the following example, we use spaces as delimiters to print the first field of the content.txt file

$cut-d ""-f 1 content.txt Ubuntu Microsoft OsX Unix FreeBSD

In the following example, we extracted multiple fields. Here, we use the colon (:) delimiter to extract the first and sixth fields from the line in the file / etc/passwd that contains the string / bin/bash.

$grep "/ bin/bash" / etc/passwd | cut-dazzlement'- F1 root:/root alvin:/home/alvin 6

To display a range of fields, you can specify the fields that begin and end, concatenated with a hyphen (-), as follows:

$grep "/ bin/bash" / etc/passwd | cut-dazzle etc/passwd'- F1-4jue 6pm 7 root:x:0:0:/root:/bin/bash alvin:x:1000:1000:/home/alvin:/bin/bash

How to complete the selected output

To complete the selected output field (that is, reverse selection), use the-- complement option. This option outputs all fields except those specified.

In the following example, output all the fields except the second field in the line containing / bin/bash in the / etc/passwd file

$grep "/ bin/bash" / etc/passwd | cut-dazzlement'--complement-f2 root:0:0:root:/root:/bin/bash

How to specify the delimiter of the output

Use-- output-delimiter to specify the delimiter for the output. The input delimiter is specified by-d, while the output delimiter and input delimiter are the same by default.

Let's first test the output without specifying an output delimiter with the following example

$cut-d:-F1 etc/passwd 7 / etc/passwd | sort | uniq-u _ apt:/usr/sbin/nologin backup:/usr/sbin/nologin bin:/usr/sbin/nologin daemon:/usr/sbin/nologin dnsmasq:/usr/sbin/nologin games:/usr/sbin/nologin gnats:/usr/sbin/nologin irc:/usr/sbin/nologin landscape:/usr/sbin/nologin list:/usr/sbin/nologin lp:/usr/sbin/nologin lxd:/bin/false

Now let's add the-- output-delimiter option to specify the output delimiter as a space:

$cut-d:-F1 etc/passwd 7-- output-delimiter'/ etc/passwd | sort | uniq-u _ apt / usr/sbin/nologin backup / usr/sbin/nologin bin/ usr/sbin/nologin daemon / usr/sbin/nologin dnsmasq / usr/sbin/nologin games / usr/sbin/nologin gnats / usr/sbin/nologin irc / usr/sbin/nologin landscape / usr/sbin/nologin list / usr/sbin/nologin lp / usr/sbin/nologin lxd / bin/false

Let's test another example, using a delimiter to have each line print one field.

We specify-- output-delimiter as the $'\ n' table line wrap.

The output is as follows:

$grep root / etc/passwd | cut-dazzlement'- F1 root 6, 7-- output-delimiter=$'\ n 'root / root / bin/bash operator / root / sbin/nologin

How to extract content as characters

The-c option can be used to extract based on character position. Note that spaces and Tab are also treated as characters.

Print the first character of each line in the context.txt file, as follows:

$cut-c 1 content.txt U M O U F

The first to seven characters of each line of the context.txt file are shown below

$cut-c 1-7 content.txt Ubuntu Microso OsX El Unix FreeBSD

Let's test again where only the start or end is specified.

Let's extract the second to the last character:

$cut-c2-content.txt buntu Linux icrosoft Windows sX El Capitan nix reeBSD

Extract the first to fourth characters:

Cut-C muri 4 content.txt Ubun Micr OsX Unix Free

How to extract based on bytes

Use the-b option to select a portion of a line by specifying the position of bytes, separate each specified location with a comma, or specify a range with a hyphen.

The following example extracts the first, second, and third bytes of each line of the content.txt file:

$cut-b 1jue 2je 3 content.txt Ubu Mic OsX Uni Fre

We can also list a range with the following command

$cut-b 1-3 content.txt Ubutu Micoso OsXEl Uni FreBSD 5-7

Some practical examples

Cut is a useful command and is often used in conjunction with other Linux or Unix commands.

For example, if you want to extract USER,PID and COMMAND from the ps command:

Ps-L u n | tr-s "| cut-d"-f 2Power3 USER PID COMMAND 0676 / sbin/agetty-o-p--\ u-- keep-baud 115200 38400 noclear tty1 linux 9600 ttyS0 vt220 0681 / sbin/agetty-o-p--\ u-noclear tty1 linux 0 23174-bash 0 26737 ps-L u n 0 26738 tr-s 0 26739 cut-d-f

To test another example, extract the total,used and free values of memory and save them to a file.

$free-m | tr-s'| sed'/ ^ Memmax _ blank'| cut-d ""-f2-4 > > memory.txt $cat memory.txt 985 86234 is all the contents of this article entitled "how to use cut commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report