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

What are the ways to use the cut command of Linux

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "what is the use of Linux's cut command?" the editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the use of Linux's cut command" can help you solve the problem.

The Linux cut command is mainly used to extract the contents of a file based on fields. Text files do not have rows / columns like a database, and sometimes we only need single-column data. Cut the identified columns according to the delimiter (for example, colon':', semicolon';', comma',', etc.).

Options for the cut command

Basic Cut syntax:

Cut OPTION... [FILE]... Options:

-f: specify which field to extract. The cut command uses "TAB" as the default field delimiter.

-d: "TAB" is the default delimiter, which can be changed to another delimiter using this option.

-complement: this option is used to exclude the specified field.

-output-delimiter: change the delimiter of the output.

How to divide

The most common option for cut is a combination of-d and-f. It basically extracts content based on specific delimiters and listed fields.

The following code uses only the delimiter: print the first field of each line in the / etc/passwd file.

[root@localhost] # cut-d':'- f 1 / etc/passwd root bin daemon adm lp sync shutdown... The following code extracts the first and sixth fields from the / etc/passwd file:

[root@localhost ~] # grep'/ bin/bash' / etc/passwd | cut-d':'- f 1Magne6 root:/root bob:/home/bob user01:/home/user01 to display the range of fields, specify the start and end fields separated by -, as follows:

[root@localhost ~] # grep'/ bin/bash' / etc/passwd | cut-d':'- f 1-4 6 root:x:0:0:/root:/bin/bash bob:x:1000:1001:/home/bob:/bin/bash user01:x:1001:1002:/home/user01:/bin/bash 7 root:x:0:0:/root:/bin/bash bob:x:1000:1001:/home/bob:/bin/bash user01:x:1001:1002:/home/user01:/bin/bash exclude the specified field

In the following code, print all the fields except the second field in the / etc/passwd file:

[root@localhost ~] # grep'/ bin/bash' / etc/passwd | cut-d':'--complement-f 2 root:0:0:root:/root:/bin/bash bob:1000:1001::/home/bob:/bin/bash user01:1001:1002::/home/user01:/bin/bash how to specify a delimiter for output content

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 delimiter. Let's take a look at what it looks like without the-output-delimiter option:

[root@localhost ~] # cut-d':'- F1 etc/passwd 7 / etc/passwd | sort adm:/sbin/nologin avahi:/sbin/nologin bin:/sbin/nologin bob:/bin/bash chrony:/sbin/nologin daemon:/sbin/nologin dbus:/sbin/nologin ftp:/sbin/nologin games:/sbin/nologin grafana:/sbin/nologin halt:/sbin/halt lp:/sbin/nologin mail:/sbin/nologin nfsnobody:/sbin/nologin nobody:/sbin/nologin ntp:/sbin/nologin operator:/sbin/nologin... Now use the-output-delimiter option, and the output delimiter is separated by a''space to see what it looks like:

[root@localhost ~] # cut-d':'- F1 sbin/nologin daemon 7-- output-delimiter=''/ etc/passwd | sort adm / sbin/nologin avahi / sbin/nologin bin/ sbin/nologin bob / bin/bash chrony / sbin/nologin daemon / sbin/nologin dbus / sbin/nologin ftp / sbin/nologin games / sbin/nologin grafana / sbin/nologin halt / sbin/halt lp / sbin/nologin mail / sbin/nologin nfsnobody / sbin/nologin nobody / sbin/nologin ntp / sbin/nologin operator / sbin/nologin about cut of Linux This is the end of the content of "what are the ways to use the command?" Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Development

Wechat

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

12
Report