(1) Its grammatical format is:
Cut [-bn] [file] or cut [-c] [file] or cut [-DF] [file]
Instructions for use
The cut command cuts bytes, characters, and fields from each line of the file and writes the bytes, characters, and fields to the standard output.
If you do not specify a File parameter, the Cut command reads the standard input. One of the-B, C, or-f flags must be specified.
Main parameters
-B: split in bytes. These byte locations ignore multibyte character boundaries, unless the-n flag is also specified.
- C: split in characters.
- D: custom delimiter, default to tab.
- F: use with-D to specify which region to display.
- N: cancels the split multibyte character. Used only with the-B flag. If the last byte of the character falls within the range of the <br/> indicated by the List parameter of the-B flag, the character is written out;
(2) What is cut generally based on? In other words, how do I tell the cut what I want to locate?
The cut command mainly accepts three positioning methods:
First, byte (bytes), with option-B
Second, character (characters), with option-C
Third, domain (fields), with option-f
(3) with "byte" positioning
For example, when you execute the PS command, you output something similar to the following:
[Rocrocket@rocrocket programming]$ who
rocrocket:0 2009-01-08 11:07
Rocrocket pts/0 2009-01-08 11:23 (: 0.0)
Rocrocket pts/1 2009-01-08 14:15 (: 0.0)
If we want to extract the 3rd byte of each row, that's it:
[Rocrocket@rocrocket programming]$ Who|cut-b 3
C
C
C
(4) If the "byte" location, I would like to extract the 3rd, 4th, 5th and 8th bytes, how to do?
-B supports the form 3-5, and multiple positioning is separated by commas. Let's take a look at the example:
[Rocrocket@rocrocket programming]$ Who|cut-b 3-5,8
Croe
Croe
Croe
However, one thing to note is that if the cut command uses the-B option, then when executing this command, the cut first sorts all the positions behind-B and then extracts it from small to large. Can not reverse the order of positioning Oh. This example will illustrate this issue:
[Rocrocket@rocrocket programming]$ Who|cut-b 8,3-5
Croe
Croe
Croe
(5) What else is similar to "3-5" such tips, enumerate it!
[Rocrocket@rocrocket programming]$ who
rocrocket:0 2009-01-08 11:07
Rocrocket pts/0 2009-01-08 11:23 (: 0.0)
Rocrocket pts/1 2009-01-08 14:15 (: 0.0)
[rocrocket@rocrocket programming]$ Who|cut-b-3
Roc
Roc
Roc
[Rocrocket@rocrocket programming]$ who|cut-b 3-
crocket:0 2009-01-08 11:07
Crocket pts/0 2009-01-08 11:23 (: 0.0)
Crocket pts/1 2009-01-08 14:15 (: 0.0)
You must have seen that,-3 represents from the first byte to the third byte, and 3-represents the third byte to the end of the line. If you are careful, you can see that both of these cases include a third byte "C".
What do you think would happen if I performed Who|cut-b -3,3-? The answer is to output the entire row, there will be no consecutive two overlapping c. Look:
[Rocrocket@rocrocket programming]$ Who|cut-b -3,3-
rocrocket:0 2009-01-08 11:07
Rocrocket pts/0 2009-01-08 11:23 (: 0.0)
Rocrocket pts/1 2009-01-08 14:15 (: 0.0)
(6) Give the simplest example of a character as a positioning mark!
The following example you have déjà vu, extracts 3rd, 4th, 5th and 8th characters:
[Rocrocket@rocrocket programming]$ who|cut-c 3-5,8
Croe
Croe
Croe
But how does it differ from-B? is the-B and-C function the same? In fact, it seems the same, just because this example is not good, who output are single-byte characters, so there is no difference between-B and-C, if you extract Chinese, the difference is seen, to see the Chinese extraction of the situation:
[Rocrocket@rocrocket programming]$ Cat Cut_ch.txt
Monday
Tuesday
Wednesday
Thursday
[Rocrocket@rocrocket programming]$ cut-b 3 cut_ch.txt
[Rocrocket@rocrocket programming]$ cut-c 3 cut_ch.txt
One
Two
Three
Four
See, with-C will be in character units, output normal; and-B will only be silly in bytes (8-bit bits) to calculate, the output is garbled.
Now that we have mentioned this knowledge, I would like to add that if you learn more, you will improve.
When multibyte characters are encountered, the-n option is used, and-n is used to tell the cut not to disassemble multibyte characters.
Examples are as follows:
[Rocrocket@rocrocket programming]$ cat Cut_ch.txt |cut-b 2
[Rocrocket@rocrocket programming]$ cat Cut_ch.txt |cut-nb 2
[Rocrocket@rocrocket programming]$ cat Cut_ch.txt |cut-nb 1,2,3
Star
Star
Star
Star
(7) What's going on with the domain? Explanation of explanation:)
Why there is a "domain" extraction, because the mentioned-B and-C only in fixed-format documents to extract information, but not fixed-format information is helpless. This is where the "domain" comes in handy. If you've looked at the/etc/passwd file, you'll find that it's not as fixed as the WHO's output, but rather fragmented. However, the colon plays a very important role in each row of the file, and the colon is used to separate each item.
We are very lucky, cut command provides such a way to extract, specifically, set the "spacer", and then set "extract the first few fields", OK!
Take the first five elements of/etc/passwd as an example:
[Rocrocket@rocrocket programming]$ cat/etc/passwd|head-n 5
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin :/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x : 4:7:lp:/var/spool/lpd:/sbin/nologin
[Rocrocket@rocrocket programming]$ cat/etc/passwd|head-n 5|cut-d:-F 1
Root
Bin
Daemon
ADM
LP
See, use-D to set the separator as a colon, and then to set the first field I want to take, and then press ENTER, all the user names are listed! Oh, have a sense of achievement!
Of course, when you set-F, you can also use a format such as 3-5 or 4-similar:
[Rocrocket@rocrocket programming]$ cat/etc/passwd|head-n 5|cut-d:-F 1,3-5
Root:0:0:root
Bin:1:1:bin
Daemon:2:2:daemon
Adm:3:4:adm
Lp:4:7:lp
[Rocrocket@rocrocket programming]$ cat/etc/passwd|head-n 5|cut-d:-F 1,3-5,7
Root:0:0:root:/bin/bash
Bin:1:1:bin:/sbin/nologin
Daemon:2:2:daemon:/sbin/nologin
Adm:3:4:adm:/sbin/nologin
Lp:4:7:lp:/sbin/nologin
[Rocrocket@rocrocket programming]$ cat/etc/passwd|head-n 5|cut-d:-f-2
Root:x
Bin:x
Daemon:x
Adm:x
Lp:x
(8) If you encounter spaces and tabs, how can you tell? I feel a little bit messy, what should I do?
Sometimes tabs are really hard to read, and there is a way to see whether a space is made up of several spaces or a tab.
[Rocrocket@rocrocket programming]$ Cat Tab_space.txt
This is tab finish.
This is several space finish.
[Rocrocket@rocrocket programming]$ sed-n L tab_space.txt
This is tab\tfinish.$
This is several space finish.$
See, if it is a tab, it will appear as the \ t symbol, and if it is a space, it will appear as it is.
You can use this method to determine the tab and space.
Notice that the character behind the sed-n is the lowercase letter of L. Oh, don't look wrong.
(9) What symbols should I use to set tabs or spaces in the cut-d?
In fact, the default spacer for cut's-D option is tab, so when you're just going to use tabs, you can simply omit the-d option and take the field directly with-F.
If you set a space to be a spacer, then that's it:
[Rocrocket@rocrocket programming]$ cat Tab_space.txt |cut-d '-F 1
This
This
Note that there is really a space between the two single quotes. Oh, can't be lazy.
Also, you can only set a space after-D, and you don't have to set multiple spaces, because cut only allows the spacer to be one character.
[Rocrocket@rocrocket programming]$ cat Tab_space.txt |cut-d '-F 1
Cut:the delimiter must is a single character
Try ' Cut--help ' for the more information.
(a) What are the deficiencies and deficiencies of cut?
Did you guess? Yes, that's when you're dealing with multiple spaces.
If some of the fields in the file are separated by a few spaces, then cut is a bit of a hassle because cut is only good at "one character spacing" text content