Cut-a small text truncation tool in Linux

Source: Internet
Author: User

Introduction

 

The cut command is a command line program in Unix. The cut command is executed in the unit of action. 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 = LIST select the specified number of LIST bytes

-C, -- characters = LIST select the specified number of LIST characters

-D, -- delimiter = DELIM uses DELIM as the content separator, instead of using the default tab as the Separator

-F, -- fields = LIST: select the part of the LIST specified by 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.

-N and-B are used together to indicate that when selecting a byte, multi-byte characters are not separated. Multi-byte characters are considered as one byte.

-S, -- only-delimited does not output rows that do not contain delimiters

-- Output-delimiter = STRING uses 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:

N indicates that N data units are specified, and the count starts from 1.

N-from the nth data unit to the end of the row

N-M from N data units to M data units (inclusive)

-M: Determine M data units (including) from the beginning of the row)

The data unit can be byte, character, or 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:

echo $PATH | cut -d ':' -f 2

To intercept all content from the second PATH to the end of the PATH variable, you can use the following method:

 

echo $PATH | cut -d ':' -f 2-

To intercept content from the beginning to the second part, you can use the following command:

echo $PATH | cut -d ‘:’ -f –3

To intercept content from Part 1 to Part 2, use the following command:

echo $PATH | cut -d ':' 2-3

 

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:

echo $PATH | cut -s -d ':' -f 3

 

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:

echo 'hello' | cut -c 3

Similarly, if you want to output characters in a specified range, you can use n-m to specify the range.

Echo 'hello' | cut-c-2 # echo 'hello' | cut-c 2-3 # output 2nd-3 Characters of the output line' hello '| cut-c 2-# 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.

echo $PATH | cut -d ':' -f 1- --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 + V # select the current line: s/\ */:/g # Use the s command to replace it. Because * in Vim, escape using a backslash

Use the cut command to process:

Shift + V # select the row where the cursor is located :! Cut-d '*'-f 1--- 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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.