Very useful Cut command

Source: Internet
Author: User

I. Cut function-remove sections from each line of files (man cut)

Is responsible for cutting data in the file, andTake each row as a processing object, similar to SED, which is very useful in processing text data.

 

Ii. Command Format and locating methods

 
Cut option... [file]...

The cut command mainly accepts three positioning methods:

1, byte (bytes, in ascending order), with option-B

2, character (characters, if it is Chinese, it may be two bytes), use option-C

3, field (fields), with option-F

 

Iii. Examples

1. Extract 3rd bytes

 
Ll/dev/SDA *
 
BRW-RW ---- 1 root disk 8, 0 2012-03-20 13:13/dev/SDA
BRW-RW ---- 1 root disk 8, 1 2012-03-20 13:13/dev/sda1
BRW-RW ---- 1 root disk 8, 2 2012-03-20 13:13/dev/sda2
BRW-RW ---- 1 root disk 8, 3 2012-03-20 13:13/dev/sda3
 
Ll/dev/SDA * | cut-B 3

W
W
W
W

2. Extract multiple bytes

Ll/dev/SDA * | cut-B 1-8, 12

BRW-RW -- 1
BRW-RW -- 1
BRW-RW -- 1
BRW-RW -- 1

3. Other tips on Byte

1-

-3

-3, 3-

4. Extract characters

It is similar to byte-based, but if it is Chinese (multiple bytes), the extraction of byte may be garbled.

5. Extract the domain (this is the most useful)

It mainly specifies a delimiter (-d) and the domain (-F) to be extracted, as follows:

 
Ll/dev/SDA * | cut-d''-F1, 7-9

BRW-RW ----/dev/SDA
BRW-RW ----/dev/sda1
BRW-RW ----/dev/sda2
BRW-RW ----/dev/sda3

6. About
When the field is extracted, cut is distinguished by tabs (\ t) by default. to distinguish between tabs and spaces in text, run the following command:

 
Sed-N LFilename
 
Xin \ tjiang \ TLI \ tzong $
Xin \ tjiang \ TLI \ tzong $
$

Cut can only process text content separated by a single character. If multiple consecutive spaces (occupying multiple fields), it is more troublesome.

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.