Cut is a selection command that analyzes a piece of data and takes out what we want.
1 syntax format
Cut [-bn] [file] or cut [-c] [file] or cut [-DF] [file]
(1) Instructions for use
The cut command cuts bytes, characters, and fields from each line of the file and writes those bytes, characters, and fields to standard output.
If you do not specify a File parameter, the Cut command reads standard input. One of the-B,-C, or-f flags must be specified.
(2) Main parameters
-B: Split in bytes. These byte locations will ignore multibyte character boundaries unless the-n flag is also specified.
-N: Cancels splitting multibyte characters. Used only with the-B flag. If the last byte of the character falls within the range of <br/> indicated by the List parameter of the-B flag, the character will be written out; otherwise, the character will be excluded.
-C: Split in characters.
-D: Custom delimiter, default is tab.
-F: Used with-D to specify which area to display
2 principle
What is cut generally based on? In other words, how do I tell cut what I want to locate?
The cut command mainly accepts three positioning methods:
(1) bytes (bytes), with option-B
(2) character (characters), with option-C
(3) domain (fields), with option-f
3 examples
(1) According to Byte cut
[[email protected] programming]$ whorocrocket:0 2009-01-08 11:07rocrocket 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:
[Email protected] programming]$ Who|cut-b 3CCC
What if I want to extract the 3rd, 4th, 5th, and 8th bytes in the "byte" position?
-B supports the notation of form 3-5, and multiple positions are separated by commas. Let's take a look at examples:
[Email protected] programming]$ Who|cut-b 3-5,8croecroecroe
But one thing to note is that if you use the-B option for the Cut command, when you execute this command, the cut will first sort all the positions after-B and then extract them. Can not reverse the order of positioning Oh. This example can illustrate the problem:
[Email protected] programming]$ Who|cut-b 8,3-5croecroecroe
(2) by character cut
[[email protected] programming]$ Cat cut_ch.txt Monday Tuesday Wednesday Thursday [[email protected] programming]$ cut-b 3 cut_ch.txt???? [[email protected] programming]$ cut-c 3 cut_ch.txt 1234
(3) by domain cut
Why is there a "domain" extraction, because the B and C just mentioned can only extract information in a fixed-format document, and for non-fixed-format information is helpless. This is where "domain" comes in handy. If you look at the/etc/passwd file, you will find that it is not the same format as the WHO output, but rather fragmented emissions. However, the colon plays a very important role in each line of the file, and the colon is used to separate each item.
We are fortunate that the cut command provides such an extraction method, specifically to set the "spacer", and then set the "Extract the first few domains", OK!
Take the first five elements of/etc/passwd as an example:
[Email protected] programming]$ cat/etc/passwd|head-n 5root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/ nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/ Sbin/nologin[[email protected] programming]$ cat/etc/passwd|head-n 5|cut-d:-F 1ROOTBINDAEMONADMLP
Use-D to set the delimiter as a colon, and then use-F to set the first domain I want to take, and then press ENTER, all the usernames are listed.
Of course, when you set-F, you can also use a format such as 3-5 or 4-Similar
[Email protected] programming]$ cat/etc/passwd|head-n 5|cut-d:-F 1,3-5root:0:0:rootbin:1:1:bindaemon:2:2:daemonadm : 3:4:admlp:4:7:lp[[email protected] programming]$ cat/etc/passwd|head-n 5|cut-d:-F 1,3-5,7root:0:0:root:/bin/ bashbin:1:1:bin:/sbin/nologindaemon:2:2:daemon:/sbin/nologinadm:3:4:adm:/sbin/nologinlp:4:7:lp:/sbin/nologin[[ Email protected] programming]$ cat/etc/passwd|head-n 5|cut-d:-f-2root:xbin:xdaemon:xadm:xlp:x
Linux Cut Command