TrumanWong

cut

Concatenate files and print to the standard output device

Supplementary instructions

cut command is used to display the specified part of the line and delete the specified field in the file. cut is often used to display the contents of a file, similar to the type command.

Description: This command has two functions. One is to display the contents of the file. It reads the files specified by the parameter file in sequence and outputs their contents to the standard output. The other is to connect two or more files. Files such as cut fl f2 > f3 will combine the contents of files fl and f2 and then put them into file f3 through the action of the output redirection character ">".

When files are large, text flashes across the screen (scrolls) and users often cannot see clearly what is being displayed. Therefore, commands such as more are generally used for split-screen display. To control scrolling, press Ctrl+S to stop scrolling; press Ctrl+Q to resume scrolling. Press Ctrl+C (Interrupt) to terminate execution of the command and return to the Shell prompt.

grammar

cut(option)(parameter)

Options

-b: Only display the content of the specified direct range in the line;
-c: Only display the specified range of characters in the line;
-d: Specify the field delimiter. The default field delimiter is "TAB";
-f: Display the contents of the specified field;
-n: used in conjunction with the "-b" option, does not split multi-byte characters;
--complement: Complement the selected bytes, characters or fields;
--out-delimiter= Field delimiter: Specify the field delimiter for the output content;
--help: Display help information for the command;
--version: Display the version information of the instruction.

Parameters

File: Specify the file to be content filtered.

Example

For example, there is a student report information, including No, Name, Mark, Percent:

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

Use the -f option to extract specified fields (the f parameter here can be simply remembered as the abbreviation of --fields):

[root@localhost text]# cut -f 1 test.txt
No
01
02
03
[root@localhost text]# cut -f2,3 test.txt
Name Mark
tom 69
jack 71
alex 68

The --complement option extracts columns other than the specified field (prints columns except the second column):

[root@localhost text]# cut -f2 --complement test.txt
No Mark Percent
01 69 91
02 71 87
03 68 98

Use the -d option to specify the field delimiter:

[root@localhost text]# cat test2.txt
No;Name;Mark;Percent
01;tom;69;91
02;jack;71;87
03;alex;68;98
[root@localhost text]# cut -f2 -d";" test2.txt
Name
Tom
jack
alex

Specify the character or byte range of the field

The cut command can display a string of characters as a column. The notation of the character field:

  • N-: From the Nth byte, character, field to the end;
  • N-M: From the Nth byte, character, field to the Mth (including M) byte, character, field;
  • -M: From the 1st byte, character, field to the M-th (including M) byte, character, field.

The above is the notation. Combine the following options to specify a range of bytes and characters as fields:

  • -b represents bytes;
  • -c represents characters;
  • -f means defining fields.

Example

[root@localhost text]# cat test.txt
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

Print characters 1 to 3:

[root@localhost text]# cut -c1-3 test.txt
abc
abc
abc
abc
abc

Print first 2 characters:

[root@localhost text]# cut -c-2 test.txt
ab
ab
ab
ab
ab

Print starting from the 5th character to the end:

[root@localhost text]# cut -c5- test.txt
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz

Print the last 5 characters:

Unfortunately, cut does not provide support for the last character. But we can achieve it by string reversal.

[root@localhost text]# cat test.txt| rev | cut -c -5 | rev
wxyz
wxyz
wxyz
wxyz
wxyz