Linux Daily one command--cut

Source: Internet
Author: User

--Sort by file size shows the first 100 rows showing the last five columns

Ll-sh|head-n 100|cut-d '-F 5-

First, the basic grammar
Cut is a selection command that, in the unit of behavior, divides the rows into fields with the specified delimiter and selects the required fields.
1. Syntax format
Cut [option] files
Option common parameters are as follows:

-D: Used to define the delimiter, the default is the TAB key, generally with-F with (if the delimiter is a space, must be two single quotation marks do have a space, is a oh, not support multiple)
-F: The field to be selected, selected according to the field set of-D shard, subscript starting from 1
-S: Indicates that no delimiter-free lines are used to remove information such as comments or headings
-C: Split in characters, you can select a specified character
-B: Split in bytes, you can select a specified byte, which ignores multibyte character boundaries (for example, kanji) unless the-n parameter is also specified
-N: To ungroup multibyte characters, only with the-B parameter, that is, if the last byte of the character falls within the range specified by the-B argument list, the character is selected, otherwise the character is excluded.
It is not difficult to see that the above parameters,-F,-C, and-B are used to represent the extraction of the specified range of data, the range is represented by the following:

N: Fetch only the nth item
N: from Nth to the end of the line
N-m: From Nth to M (including m)
-M: From the first item to the M item (including m items)
-: All items from the start of the first entry to the end
Second, the application example
1. Basic usage
Use the-D and-f combinations to select fields, which are illustrated in path as an example:

Copy Code code example: #echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/changquan.scq/bin

(1) Select a 2nd path:

Copy Code code example: #echo $PATH | Cut-d:-f2
/usr/local/bin

(2) Select all paths after the 2nd start:

Copy Code code example: #echo $PATH | Cut-d:-f2-
/usr/local/bin:/bin:/usr/bin:/home/changquan.scq/bin

(3) Select the 2nd to 4th path, including the 4th path:

Copy Code code example: #echo $PATH | Cut-d:-f2-4
/usr/local/bin:/bin:/usr/bin

(4) Select a path from 1th to 4th, including the 4th path:

Copy Code code example: #echo $PATH | Cut-d:-f-4
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin

(5) Select all paths from the 1th to the last path:

Copy Code code example: #echo $PATH | Cut-d:-f-
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/changquan.scq/bin
(6) Select the 1th path and the 3rd path:

Copy Code code example: #echo $PATH | Cut-d:-f1,3
/usr/kerberos/bin:/bin

(7) Select the 1th to 3rd path and the 5th path:

Copy Code code example: #echo $PATH | Cut-d:-f1-3,5
/usr/kerberos/bin:/usr/local/bin:/bin:/home/changquan.scq/bin
2. Character positioning
This is illustrated by the WHO example:

Copy Code code example: #who
CHANGQUAN.SCQ pts/02014-05-13 16:21 (10.62.50.159)
CHANGQUAN.SCQ pts/12014-05-13 17:53 (10.62.50.159)
CHANGQUAN.SCQ pts/22014-05-13 18:09 (10.62.50.159)
CHANGQUAN.SCQ pts/32014-05-13 18:34 (10.62.50.159)
CHANGQUAN.SCQ pts/42014-05-13 18:37 (10.62.50.159)
CHANGQUAN.SCQ pts/52014-05-13 19:08 (10.62.50.159)

(1) Extract the 3rd byte of each line:

Copy Code code example: #who | Cut-c3
A
A
A
A
A

(2) Extract the 1th byte from the beginning to the 3rd byte, including the 3rd byte:

Copy Code code example: #who | Cut-c-3
Cha
Cha
Cha
Cha
Cha

(3) Extracts all bytes starting from the 3rd byte to the end, including the 3rd byte:

Copy Code code example: #who | cut-c3-
ANGQUAN.SCQ pts/02014-05-13 16:21 (10.62.50.159)
ANGQUAN.SCQ pts/12014-05-13 17:53 (10.62.50.159)
ANGQUAN.SCQ pts/22014-05-13 18:09 (10.62.50.159)
ANGQUAN.SCQ pts/32014-05-13 18:34 (10.62.50.159)
ANGQUAN.SCQ pts/42014-05-13 18:37 (10.62.50.159)
ANGQUAN.SCQ pts/52014-05-13 19:08 (10.62.50.159)

(4) Extract the entire row, and the 3rd byte does not overlap:

Copy Code code example: #who | cut-c-3,3-
CHANGQUAN.SCQ pts/02014-05-13 16:21 (10.62.50.159)
CHANGQUAN.SCQ pts/12014-05-13 17:53 (10.62.50.159)
CHANGQUAN.SCQ pts/22014-05-13 18:09 (10.62.50.159)
CHANGQUAN.SCQ pts/32014-05-13 18:34 (10.62.50.159)
CHANGQUAN.SCQ pts/42014-05-13 18:37 (10.62.50.159)
CHANGQUAN.SCQ pts/52014-05-13 19:08 (10.62.50.159)

(5) Extract the 3rd to 5th and 8th bytes of each line:

Copy Code code example: #who | cut-c3-5,8
Anga
Anga
Anga
Anga
Anga

3, byte positioning
The Who is still the example of:
Extracts the 3rd to 5th and 8th bytes of each row:

Copy Code code example: #who | cut-b3-5,8
Anga
Anga
Anga
Anga
Anga

I look at the B and c no difference, in fact, the two in single-byte characters (letters) are basically the same, and in multibyte characters (Chinese characters) there is a great difference, here is the Chinese character extraction as an example of:

Copy Code code example: #vi test.txt
Monday
Tuesday
Wednesday
#cut-b3 Test.txt
?
?
?
#cut-c3 Test.txt
One
Two
Three

It is not difficult to see from the above example that the-C output is normal in characters, and-B output garbled in bytes.
Of course,-B is not a drug-free when encountering multibyte characters, and a parameter-n can be used to tell the cut not to disassemble multibyte characters:

Copy Code code example: #cut-NB 2

#cut-NB
Star
Star
Star

Linux Daily one command--cut

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.