Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the cut command

Shulou Source: shulou.com Published: 2022-06-01 14:21:11 09月11日 Update

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.

Tags: Separator field command content output code file knowledge face appearance article input same one line value common use situation easy to operate article easy to understand Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux Apple Shulou Technology Xiaomi