Introduction
The cut command is a command line program in Unix. The cut command isLine. The cut command processes standard input. Therefore, you can use the pipeline to process the text.
Command Format
Cut Option... [File]…
The option part of the cut command can be the following options:
-B, -- bytes = LISTSelect the specified number of LIST bytes
-C, -- characters = LISTSelect a specified number of LIST characters
-D, -- delimiter = DELIMUse DELIM as the content separator instead of the default tab
-F, -- fields = LISTSelect the part of the LIST specified by the LIST and output the selected content. If the character does not contain a separator, the output is the same, unless the-s option is specified.
-NThis parameter is used with-B to indicate that when you select a byte, multi-byte characters are not separated. Multi-byte characters are considered as one byte.
-S, -- only-delimitedOutput rows that do not contain delimiters
-- Output-delimiter = STRINGUse STRING as the Separator in the output content, rather than the Separator in the original standard input.
Note: For option-B,-c,-f, only one of the commands can be specified.
The output sequence of command results is the same as that of data to be processed.
A list can be a range or multiple ranges separated by commas.
LIST format:
NSpecifies N data units. The count starts from 1.
N-From the nth data unit to the end of the row
N-MFrom N data units to M data units (including)
-MM data units (including) starting from the row)
The data unit can be:Byte, character, data block
Note: If FILE or FILE is not specified in the command, the standard input is read by default.
Usage
In Linux, the environment variable PATH contains the search PATH for variables separated by ":". If we need to extract the second PATH, we can use the following method:
$PATH | -d -f
To intercept all content from the second PATH to the end of the PATH variable, you can use the following method:
$PATH | -d -f -
To intercept content from the beginning to the second part, you can use the following command:
$PATH | -d ‘:’ -f –
To intercept content from Part 1 to Part 2, use the following command:
$PATH | -d -
If the same operation does not contain any delimiters, we may not need to process the same operation. cut is output as is by default. If you do not need them,
You can use the '-S' option to block rows that do not contain the specified separator:
$PATH | -s -d -f
The above-mentioned-d option can be used when there are obvious delimiters, and the line can be easily intercepted during the processing of text lines in an obvious format. In addition to the delimiter used to intercept and separate text, you can also intercept the specified number of characters for character text without the separator format, that is, the-c option:
Extract the 3rd bytes in a line of text:
| -c
Similarly, if you want to output characters in a specified range, you can use n-m to specify the range.
|-C-# output content from the second character to the end of the line
When outputting, if you need to specify another separator to separate the output content, to replace the original separator, you can use -- output-delimiter for processing.
$PATH | -d -f - --output-delimiter=
The output PATH variable uses * as the separator instead of: As the separator. Is it convenient?
When cut encounters Vim
Imagine whether the cut command can be used with Vim to complete many domineering operations! See:
Assume that the text is being edited in Vim:
I want to change * of the number to colon: What should I do? Change one by one ?, You may think of replacing the mode in Vim, which is indeed a good method. If Vim is combined with the cut command, it can achieve the same effect:
Use the replace command in Vim mode:
Shift +/\ */:/g # Replace with the s command. Because * in Vim, it has a special meaning and needs to be escaped using a backslash.
Use the cut command to process:
Shift+! -d -f - --output-delimiter=
The execution results of the two commands are the same:
Therefore, using the cut command with Vim can also be an effective method for text editing. Isn't it a manifestation of the KISS mode in Unix?