Linux under the cut command

Title: Using the cut command

Action: Cut command file in units, in bytes, characters specified field separator cut rows, required to extract segments of the content.

First, use :

      cut [-bn] [file] 或cut -c [file] 或cut -[df] [file]

     command and writes the cut line cut from each byte, character, and these files byte field, and character fields to standard output. If you do not specify a File Second, the parameters, cut command reads standard input. You must specify -b, -c or one of the -f flag.

Second, the parameters:

     -b (byte): divided in byte unit. These byte positions will ignore multi-byte character boundary, unless the -n flag is also specified

  -c (character): the character is divided into units

  -d (delimiter): Custom delimiter, default tab

  -f (fileds): used with -d, which specifies the display area

  -n: Cancel split multi-byte characters. Only used with the -b flag. If the last-byte character falls indicated by the List parameter -b flags change

           Within the scope of the line, the character will be written out; otherwise, the character will be excluded.

Third, examples

   1. extracts the character in the specified range

           (1) cut -c n1-n2 filename (n1 and n2 are taken to specify the range of characters, n1 is the starting position, n2 is the OFF position specified filename file name)

            filename:number.txt

            10 10

            20 20
            14 14
            11 11

              Command: cut -c 1-2 number.txt

                 Output:

           10
           20
           14
           11

       Description: The contents of the documents, then characters, kanji character length in Unix in UTF-8 encoding 3 is occupied, it is necessary to extend the length of a character corresponding to 3.

     (2) For example: If the file weekday reads:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

   So if you want to extract the first kanji "star" of the command is: cut -c 1-3 tmp

   2. Extract byte within the specified range

      (1) number.txt extracts two bytes of content 1-2

       Command: cut -b 1-2 number

                Output:

10
20
14
11

   3. The specified field separator to extract the contents of a field (-d -f and used in conjunction with)

           (1) is in accordance with the contents of $ PATH: dividing, if you want extracted by: dividing the contents of the second field

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

             Output: / usr / local / sbin

Guess you like

Origin www.cnblogs.com/vs-kaka/p/11266794.html