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

2025-01-28 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 how to use the cut command, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this cut command how to use the article. Let's take a look.

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/passwdrootbindaemonadmlpsyncshutdown...

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 rootrig6 rootbuser01

"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 Magi 6Graves 7rootVane 0RAPHER 0RAPHER 0ROFUBING BASHBOBUBG 1001 HomeBUBJUBG 1001 HomeBUBJUBG BINBING 1002 HomeUser01RUBING BHH excludes 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 2root:0:0:root:/root:/bin/bashbob:1000:1001::/home/bob:/bin/bashuser01:1001:1002::/home/user01:/bin/bash how to specify a delimiter for output

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 | sortadm:/sbin/nologinavahi:/sbin/nologinbin:/sbin/nologinbob:/bin/bashchrony:/sbin/nologindaemon:/sbin/nologindbus:/sbin/nologinftp:/sbin/nologingames:/sbin/nologingrafana:/sbin/nologinhalt:/sbin/haltlp:/sbin/nologinmail:/sbin/nologinnfsnobody:/sbin/nologinnobody:/sbin/nologinntp:/sbin/nologinoperator:/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 output-delimiter=''/ etc/passwd | sortadm / sbin/nologinavahi / sbin/nologinbin / sbin/nologinbob / bin/bashchrony / sbin/nologindaemon / sbin/nologindbus / sbin/nologinftp / sbin/nologingames / sbin/nologingrafana / sbin/nologinhalt / sbin/haltlp / sbin/nologinmail / sbin/nologinnfsnobody / sbin/nologinnobody / sbin/nologinntp / sbin/nologinoperator / sbin/nologin this is the end of the article on "how to use cut commands". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use cut commands". If you want to learn more, 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: 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