SoFunction
Updated on 2025-03-10

Detailed explanation of the usage of Shell cut

cut is used to process an object in each behavior, and this mechanism is the same as sed.

Cut accepts three positioning methods:

1)byte: -b

2)characters: -c

3)fields: -d

eg: Extract the bytes of 3, 4, 5, 9:cut -b 3-5,9

Note: If the cut command uses the -b option, all positionings after -b will be sorted from small to large when executing it, and then extracted it. The order cannot be reversed.

-3 means from the first byte to the third byte;

3- Indicates from the third byte to the end.

For Chinese extraction, -c will be in characters and the output is normal; -b will be calculated in bytes (8-bit binary).

When multibyte characters are encountered, the -n option is used, and the multibyte characters will not be disassembled.cut -nb 1,2,3

For non-fixed format information, a domain is required. Before, you need to set the spacer and then extract the number of fields.

cut -d : -f 1

-d sets the spacer to:, -f 1 is to extract the first field.

Note: How to identify spaces and tab characters if you encounter them?

First check whether this space consists of spaces or tab characters:

cat tab_space.txt
sed -n l tab_space,txt

If it is a tab position (TAB), it will be displayed, and if it is a space, it will be displayed as it is. (l after n in sed is L in lowercase)

cut -d What symbol is used to represent tabs or spaces?

cut -d The default interval is tab characters and can be omitted. If space is set as a spacer, cut -d ' ' -f 4

(There must be a space in two single quotes)

And you can only set one space after -d, and multiple spaces are not allowed. The allowable spacer character for cut is a character.

Summarize

The above is the usage of Shell cut that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!