Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the ways to use the cut command of Linux

Shulou Source: shulou.com Published: 2022-06-01 19:44:26 09月28日 Update

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.

Tags: Separator field command content file output method usage code knowledge face data appearance industry input different practical same one line colon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Apple Redmi Docker