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-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article shows you how to use the Cut command in Linux. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Cut commands and syntax

The basic syntax for cut commands is as follows:

Cut OPTION... [FILE]...

Let's take a look at this option, and there is no option that this command does not work.

Clipping options:

F: extract through the specified field. The cut command uses "TAB" as the default field delimiter.

The defaulted delimiter is the DRV _ delimiter', which allows you to use a specific delimiter.

B: extract by specifying a byte. You can also specify a byte range.

C: split by character. This can be a comma-separated list of numbers or a range of numbers separated by a hyphen (-). -complement: used to supplement the selection

Output-delimiter: to change the output delimiter, use the option-output-delimiter = 'delimiter'.

-only-delimited:Cut will not output lines without delimiters

In this tutorial, we will use the following text file named "content.txt" and the / etc / passwd file to illustrate our example.

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

How to cut with a delimiter

The most commonly used cut option is a combination of-d and-f, which basically extracts content based on specific delimiters and listed fields.

For example, the following only outputs the first field of each line in the'/ etc / passwd' file using the delimiter (:).

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

In the following example, we use a space ("") as the delimiter and remove the first field from the file named 'content.txt'.

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

This example extracts multiple fields from a specific file. Here, we use the colon (:) delimiter to extract the first and sixth fields from the file "/ etc / passwd", which contains the string "/ bin / bash":

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

To display the field range, specify the start and end fields separated by hypen (-), as follows:

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

How to supplement output selection

To supplement the list of selected fields, use the-- complement option. This option is used to select all fields except the specified field.

In the following example command, all fields except the second field in the'/ etc / passwd' file are output:

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

How to specify the output separator:

To specify the output delimiter, use the-- output-delimiter option. The input delimiter is specified by the-d option, and by default, the output delimiter is the same as the input separator.

Let's first take a look at the case where the output does not use output delimiters, as follows:

$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, I have added the-- output-delimiter option and replaced the input delimiter colon (:) with the output delimiter "SPACE", as follows:

$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 look at another example, where we use the output delimiter to output on each field of the new line.

Here, we use-- output-delimiter as the $'\ nline to represent the new line.

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 cut by character

Used to cut by character position. Remember, "TABS" and "space" are also considered characters.

To output the first character from each line of a file named content.txt, use the following command:

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

In the following example, we will display the characters 1 to 7 (range) for each line in the file:

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

Let's see how to select a column by a specific start or end position.

To extract a column from the second character to the last character:

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

To extract a column from the first character to the fourth character:

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

How to cut by byte

Use the-b option to select a portion of a row by specifying the byte position with numbers separated by commas (,). Use hyphens to specify a byte range.

The following example cuts out the first, second, and third bytes of the file named "content.txt":

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

We can also list ranges using the following command:

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

Some practical examples

Cut is the most practical way to combine different Linux or Unix commands.

For example, you would use the ps command to extract "USER", "PID" and "COMMAND" as follows:

Ps-L u n | tr-s "| cut-d"-f 2Power3 USER PID COMMAND 14-USER PID COMMAND 0676 / sbin/agetty-o-p--\ u-- keep-baud 115200 38400 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 2jue 3p14-

Let's give another example to extract the "total", "used", and "free" values of memory and save them to a text file using several commands:

$free-m | tr-s''| sed'/ ^ Memplet'| cut-d ""-f2-4 > > memory.txt Output $cat memory.txt 985 86234

Conclusion

The cut command can be passed along with many other Linux or Unix commands. You can pass one or more filters through a pipe for other text processing.

One of the limitations of the cut command is that it does not support specifying multiple characters as delimiters. Multiple spaces are counted as multiple field delimiters, and you must use the tr command before cutting to get the desired output.

The above is how to use the Cut command in Linux. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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: 295

*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