Linux cut Command Explanation

  • 2021-08-17 01:47:52
  • OfStack

The role of the cut command in Linux and Unix is to intercept 1 part of every 1 line in the file and output it to standard output. We can use cut command from 1 line string in bytes, characters, fields (delimiters) and other units to intercept 1 part of the content out.

In this article, we use a few examples to understand the use of cut commands, which are also very common in our daily work.

Cut commands and syntax

The basic syntax of the cut command is as follows:

$ cut OPTION... [FILE]...

Let's first look at some of the options of cut under 1. The cut command must specify options before it can be executed.

-f Extracts the specified field. The cut command uses Tab as the default delimiter.

-d Tab is the default delimiter, and you can specify your own delimiter with this 1 option.

-b Extracts the specified bytes, or specifies a range.

-c Extracts the specified character, either a list of numbers separated by commas or a range of numbers separated by hyphens.

�complement Supplement the selected part, that is, reverse selection.

�output-delimiter : The delimiter used when modifying the output.

--only-delimited Do not output columns that do not contain delimiters.

We use the following name context.txt Text file of and text file of /etc/passwd File as an example.


$ cat content.txt 
Ubuntu Linux
Microsoft Windows
OsX El Capitan
Unix
FreeBSD

How do I specify a delimiter

The most commonly used options are -d And -f This will be based on the combination of -d The specified separator and the -f To extract the content from the fields listed.

For example, in this example, only the first field of every line of the/etc/passwd file is printed with the separator of :


$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
alvin
liangxu
...

In the following example, we print the first field of the content. txt file with spaces as separators


$ cut -d " " -f 1 content.txt 
Ubuntu
Microsoft
OsX
Unix
FreeBSD

In the following example, we extracted multiple fields. Here, we use the colon (:) delimiter to extract the first and sixth fields from the line in the file/etc/passwd that contains the string/bin/bash.


$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
alvin:/home/alvin

To display a range of fields, you can specify the starting and ending fields, concatenated by a hyphen (-), as follows:


$ grep "/bin/bash" /etc/passwd | cut -d':' -f1-4,6,7
root:x:0:0:/root:/bin/bash
alvin:x:1000:1000:/home/alvin:/bin/bash

How to complete the selected output

To complete the field of selection output (that is, reverse selection), use --complement Option. This 1 option outputs all fields except the specified fields.

In the following example, the output file the/etc/passwd contains all the fields in the line the/bin/bash except for the second field:


$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -f2
root:0:0:root:/root:/bin/bash

How to Specify Output Separators

Use --output-delimiter You can specify the separator for the output. The delimiter entered is defined by the -d The output delimiter and input delimiter are 1 by default.

Let's test the output without specifying the output separator with the following example.


$ cut -d: -f1,7 /etc/passwd | sort | uniq -u
_apt:/usr/sbin/nologin
backup:/usr/sbin/nologin
bin:/usr/sbin/nologin
daemon:/usr/sbin/nologin
dnsmasq:/usr/sbin/nologin
games:/usr/sbin/nologin
gnats:/usr/sbin/nologin
irc:/usr/sbin/nologin
landscape:/usr/sbin/nologin
list:/usr/sbin/nologin
lp:/usr/sbin/nologin
lxd:/bin/false

Now we add --output-delimiter Option to specify the output separator as a space:


$ cut -d: -f1,7 --output-delimiter ' ' /etc/passwd | sort | uniq -u
_apt /usr/sbin/nologin
backup /usr/sbin/nologin
bin /usr/sbin/nologin
daemon /usr/sbin/nologin
dnsmasq /usr/sbin/nologin
games /usr/sbin/nologin
gnats /usr/sbin/nologin
irc /usr/sbin/nologin
landscape /usr/sbin/nologin
list /usr/sbin/nologin
lp /usr/sbin/nologin
lxd /bin/false

Let's test another example, using separators to print 1 field per 1 line.

We will --output-delimiter Specify as $'\n' Table wrap.

The output is:

$ grep root /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'\n'
root
/root
/bin/bash
operator
/root
/sbin/nologin

How to extract content as characters

-c Option can be used to extract according to character position, pay attention to spaces and -b1 It is also processed in characters.

Print the first character of every line of the context. txt file as follows:


$ cut -c 1 content.txt
U
M
O
U
F

Characters 1 to 7 per line of the context. txt file are shown below;


$ cut -c 1-7 content.txt
Ubuntu
Microso
OsX El
Unix
FreeBSD

Let's test 1 again and specify only the starting or ending position.

The second to last characters are extracted below:


$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
alvin
liangxu
...
0

Extract Characters 1 through 4:


$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
alvin
liangxu
...
1

How to extract according to bytes

Use -b Option selects a part of a line 1 by specifying the position of bytes, separating each specified position with a comma, or using a hyphen - Specify 1 range.

The following example extracts the first, second, and third bytes of every line of the content. txt file:


$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
alvin
liangxu
...
2

We can also list 1 range with the following command;


$ cut -b 1-3,5-7 content.txt 
Ubutu 
Micoso
OsXEl 
Uni
FreBSD

1 Some practical examples

cut is a practical command that is often used in conjunction with other Linux or Unix commands.

For example, if you want to extract USER, PID, and COMMAND from the ps command:


$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
alvin
liangxu
...
4

Test another example, extract the total, used and free values from memory and save them to a file.


$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
alvin
liangxu
...
5

Summarize

The cut command can be piped with many other Linux or Unix commands. One or more filters can be piped for additional text processing.

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


Related articles: