cut of Linux command -- split the string, display or delete the contents of the specified field, and merge the contents of the file

Posted by jwstockwell on Tue, 11 Jan 2022 00:59:16 +0100

1, Command introduction

The command cut can be used to split the string and display the content of the specified range. The text content can be displayed according to the specified byte range, character range and split field range. You can also merge the contents of multiple files.

2, Common options

optionexplain
-bDisplays the characters in the specified byte range in the line in bytes
-cDisplays the characters in the specified character range in the line in character units
-dSpecifies the field separator. The default field separator is TAB. The field separator must be enclosed in quotation marks, either single quotation marks or double quotation marks. Spaces are allowed between the separator and option - d
-fDisplays the contents of the specified field
-nUsed with the - b option to not split multi byte characters
--output-delimiter=<STRING>Specify the field separator of the output. No space is allowed between the separator and the option. The separator can be without quotation marks, but if the specified separator is a special symbol, it must be quoted
--helpDisplays help information for the command
--versionDisplays the version information of the instruction
--complementIt is used to exclude the character content in the specified range. The actual effect is inconsistent with the meaning of the word complex. It can only be considered that the foreigner's understanding is against the sky!

3, Command example

Displays the contents of the specified file according to the specified byte range

File text Txt is as follows:

[root@htlwk0001host test]# cat text.txt
hello shell world!
dfdskklllllf
dfsfdsf
shell world
sdffdsfdsfd
dfsfdfdsfds
fsdfdsfdsfdsf
fdsfdsfdsfd

Display file text Txt contents of the 1st to 8th bytes of each line:

[root@htlwk0001host test]# cut -b1-8 text.txt
hello sh
dfdskkll
dfsfdsf
shell wo
sdffdsfd
dfsfdfds
fsdfdsfd
fdsfdsfd

Note: - b indicates that the characters in the specified byte range in the line are displayed in bytes

Display file text Txt characters in the 1st byte and 8th byte of each line:

[root@htlwk0001host test]# cut -b1,8 text.txt
hh
dl
d
so
sd
ds
fd
fd

Specify the field separator to display the contents of the specified field

In fact, it is a user-defined separator to segment the content of each line of text, and then select the content of some fields after segmentation for display.

[root@linuxcool ~]# cat student2.txt 
No;Name;Mark;Percent 
01;tom;69;91 
02;jack;71;87 
03;alex;68;98 

Specify a semicolon; As a separator, display the contents of the second field:

[root@linuxcool ~]# cut -f2 -d";" student2.txt 
Name 
tom 
jack 
alex 

Note: the field separator can be without quotation marks, and spaces are allowed between the separator and option - d.

Displays the contents of the specified field

The default field separator is TAB, so each field of text content has been separated by TAB. You can directly use the option - f to display the content of the specified field:

[root@linuxcool ~]# cat student.txt 
No Name Mark Percent 
01 tom   69   91 
02 jack  71   87 
03 alex  68   98  

Use the - f option to display the contents of the second field:

[root@linuxcool ~]# cut -f 2 student.txt 
Name
tom 
jack
alex 

Display the contents of the 2nd and 3rd fields:

[root@localhost text]# cut -f2,3 test.txt
Name Mark
tom 69
jack 71
alex 68

Displays the contents of the specified file according to the specified character range

File test Txt is as follows:

[root@linuxcool ~]# cat test.txt 
abcdefghijklmnopqrstuvwxyz 
abcdefghijklmnopqrstuvwxyz 
abcdefghijklmnopqrstuvwxyz 
abcdefghijklmnopqrstuvwxyz 
abcdefghijklmnopqrstuvwxyz 

Display the contents of characters 1 to 3:

[root@linuxcool ~]# cut -c1-3 test.txt 
abc 
abc 
abc 
abc 
abc 

Display the first 2 characters of each line:

[root@linuxcool ~]# cut -c-2 test.txt 
ab 
ab 
ab 
ab 
ab 

Display content from the beginning to the end of the fifth character:

[root@linuxcool ~]# cut -c5- test.txt 
efghijklmnopqrstuvwxyz 
efghijklmnopqrstuvwxyz 
efghijklmnopqrstuvwxyz 
efghijklmnopqrstuvwxyz 
efghijklmnopqrstuvwxyz 

explain:

Range expressionexplain
n-From the nth byte, character, field to the end
n-mFrom the nth byte, character and field to the m-th byte, character and segment, including n and m
-mFrom the 1st byte, character, field to the m-th byte, character, field

Displays the contents of fields other than the specified field

You can use the option -- complex to extract columns other than the specified fields, that is, print the contents of columns other than the second column:

[root@linuxcool ~]# cut -f2 --complement student.txt 
No Mark Percent 
01  69   91 
02  71   87 
03  68   98 

Splits the fields in the output content with the specified output separator

You can use the option -- output delimiter to specify the field separator when outputting content. For example, specify the field separator that matches # as output, as shown below:

[root@htlwk0001host test]# cut -d';' -f2,4 --output-delimiter='#' text2.txt
Name#Percent 
tom#91 
jack#87 
alex#98 

explain:
-d';' Indicates the semicolon in the input data stream; The text content of each line is divided as a field separator, and -- output delimiter = '#' indicates that the content is displayed with # as a field separator when the content is output.

Merge the contents of multiple files

Merge the contents of files f1 and f2, and then put the merged contents into file f3 by overwriting the output redirection > as follows:

[root@htlwk0001host test]# cut f1 f2 > f3

Topics: Linux cmd