“This is the 18th day of my participation in the Gengwen Challenge. For more details, see” Gengwen Challenge “.

Cut is a selection command that intercepts the desired information through the information of a certain line and the control of parameters.

The basic grammar

Its syntax format is:

Cut [-bn] [file] or cut [-c] [file] or cut [-df] [file]Copy the code

Instructions:

The cut command cuts bytes, characters, or fields from each line of the file and writes them to standard output.

If the file argument is not specified, the cut command reads standard input. One of the -b, -c, or -f flags must be specified.

Main parameters are as follows:

  • -b: The partition is performed in bytes
  • -c: The partition is based on characters
  • -d: custom delimiter, default is TAB symbol
  • -fAnd:-dTo specify which parts to display when separated by a delimiter
  • -n: Unsplit multi-byte characters, only and-bLogo is used together.

Selected on the basis of

Accept three options for positioning

  1. Byte location, which corresponds to the option-b
  2. Characters (characters) positioning, corresponding options-c
  3. Domain (Fileds), the corresponding option-f

The following shows how to use it

Locate by byte

When we execute a new TXT file, the command is who.txt and its contents are as follows

superStar console  May 27 11:05
superStar ttys000  Jun  9 18:41
superStar ttys012  May 27 11:07
Copy the code

Cat who.txt will output something like this:

➜  shellLearn  cat who.txt
superStar console  May 27 11:05
superStar ttys000  Jun  9 18:41
superStar ttys012  May 27 11:07
Copy the code
  • Intercept the third character of each line

We can perform the cat who. TXT | the cut – 3 b, the effect is as follows

➜ shellLearn cat who. TXT | the cut - 3 p p p bCopy the code

If you still don’t understand | command meaning, we can see this article magic sword: at the beginning of the shell

  • Intercepts the first to ninth, 11th and 13th characters of each line

    1. -bSupport for continuous positioning, such as the first and ninth bits, can be abbreviated to 1-9
    2. -bSupport multiple positioning, multiple positioning is separated by commas
➜ shellLearn cat who. TXT | the cut - 1-9 b,11,13 superStarcn superStarty superStartyCopy the code

-b indicates that flag bits are sorted from small to large and then intercepted, and then executed in a disordered order. The effect is the same as above.

➜ shellLearn cat who. TXT | the cut - b 11,13,1-9 superStarcn superStarty superStartyCopy the code
  • Intercepts the string before or after bit 9 of each line

Before 9th place (including 9th place), cut-B-9

➜ shellLearn cat who. TXT | the cut - b - 9 superStar superStar superStarCopy the code

After the 9th place (including the 9th place), cut – B 9-

➜ shellLearn cat who. TXT | the cut - 9 - b r console May 27 his r ttys000 Jun 9 now r ttys012 11:07 May 27Copy the code

If you cut -b-9,9- :

➜ shellLearn cat who. TXT | the cut - b - 9, 9 - superStar console May 27 his superStar ttys000 Jun 9 now superStar ttys012 May 27 11:07Copy the code

The ninth digit is not repeated, and the entire line is displayed.

Position by character

If the text content is pure single-byte characters, character positioning and byte positioning have the same effect. Such as:

➜ shellLearn cat who. TXT | the cut - b 3 p p p ➜ shellLearn cat who. TXT | the cut - c 3 p p pCopy the code

Create a week.txt containing the following strings.

Monday Tuesday Wednesday ThursdayCopy the code

By intercepting the output stream of cat week.txt, you can see the difference between the two commands. -b displays garbled characters, and -c displays the desired effect

➜ shellLearn cat week. TXT | the cut - 3 b � � � � ➜ shellLearn cat week. TXT | cut 3, 1234 - cCopy the code

Because -c is in character, -b is only in byte to calculate, output is garbled

To byte location, with-n

As we saw above, -b displays garbled characters when it encounters multi-byte characters. But it can also be paired with the -n option, which tells cut not to break up multi-byte characters.

Here’s an example:

➜ shellLearn cat week. TXT | the cut - b 1 � � � � ➜ shellLearn cat week. TXT | the cut - nb 1 ➜ shellLearn cat week. TXT | the cut - nb 1-3 star Star Star %Copy the code

Note: the above effect is on the Mac effect, Linux may be different, especially at the end of % is not known why

Locate by domain (Fileds)

For the above format, -B or -c works fine. However, if we encounter something in the format /etc/passwd, we take five lines from the bottom:

➜ shellLearn cat/etc/passwd | tail - n5 _fpsd: * : 265:265 - FPS Daemon: / var/db/FPSD: / usr/bin /false
_timed:*:266:266:Time Sync Daemon:/var/db/timed:/usr/bin/false
_nearbyd:*:268:268:Proximity and Ranging Daemon:/var/db/nearbyd:/usr/bin/false
_reportmemoryexception:*:269:269:ReportMemoryException:/var/db/reportmemoryexception:/usr/bin/false
_driverkit:*:270:270:DriverKit:/var/empty:/usr/bin/false
Copy the code

It seems to have no rules, but there are rules. It is a concatenated string of:, colons are used to separate items, but each line varies greatly in length.

So if we want to take something before the first colon, something between the second and the third colon, what do we do.

At this time, to the domain location will come in handy, simply put, is to set the interval, and then set the extraction of the domain, it is good.

➜ shellLearn cat/etc/passwd | tail - n5 | the cut - d: The -f 1 _fpsd _timed _nearbyd _reportmemoryexception _driverkit ➜ shellLearn cat/etc/passwd | tail - n5 | the cut - d: -f 3 265 266 268 269 270Copy the code

The -d command is used to set the interval to a colon, and the -f command is used to extract the required field. Press enter again, there it is. Be happy.

-f also supports continuous domain interception and multiple domain interception. The following is an example:

➜ shellLearn cat/etc/passwd | tail - n5 | the cut - d: 266-1, 3 f _fpsd: 265 _timed: _nearbyd: 268 _reportmemoryexception: 269 _driverkit: 270 ➜ shellLearn cat/etc/passwd | tail -n5 | cut -d : - 1, 3-5 _fpsd f, 265:265 - FPS Daemon _timed: 266-266: the Time Sync Daemon _nearbyd: 268-268: Proximity and held the Daemon _reportmemoryexception: 269-269: ReportMemoryException _driverkit: 270-270: DriverKit ➜ shellLearn cat/etc/passwd | tail -n5 | cut -d : -f -3 _fpsd:*:265 _timed:*:266 _nearbyd:*:268 _reportmemoryexception:*:269 _driverkit:*:270Copy the code

cutThe short board

Cut is cumbersome when dealing with fields with multiple Spaces spaced, but it is good at handling text content “one character apart.”