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

An example of using the cut command

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The cut command is used for text processing. By selecting a column, you can use this command to extract a portion of the text from the file. 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 Summary

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

Original address of this article: https://www.linuxprobe.com/cut-example.html

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: 242

*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