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 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.
Main Parameters
-B: Split in bytes. These byte locations will ignore multibyte character boundaries unless the-n flag is also specified.
-C: Split in characters.
-D: Custom delimiter, default is tab.
-F: Used with-D to specify which area to display.
-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.
Specify the character or byte range of a field
The cut command can display a string of characters as a column, with the notation of the character field:
- n: From Nth Byte, character, field to end;
- n-m: bytes, characters, fields from Nth Byte, character, field to M (including m);
- - M: bytes, characters, and fields from the 1th byte, character, field to the first m (including m).
Ii. examples
Taking the first five elements of/etc/passwd as an example
[[email protected] ~]# head-5/etc/ passwd root:x: 0 : 0 : root:/root:/bin/ bashbin:x: 1 : 1 : bin:/bin:/sbin/ nologindaemon:x: 2 : 2 :d aemon:/sbin:/sbin/nologinadm:x: 3 : 4 : Adm:/var /adm:/sbin/nologinlp:x: 4 : 7 : Lp:/var /spool/lpd:/sbin/nologin
Intercept user name:-D to set the delimiter as a colon, and then use-F to set the first field I want to take
[email protected] ~]# head-5/etc/passwd |cut-d:- F1ROOTBINDAEMONADMLP
Intercept 1th, 3 to 5, field
[email protected] ~]# head-5/etc/passwd |cut-d:-f1,3-5 Root: 0:0: rootbin:1:1: Bindaemon:2:2: Daemonadm:3:4: ADMLP:4:7: LP
Intercept 1 to 3 characters
[[email protected] ~]# head-5 /etc/passwd |cut-c1-3 ROOBINDAEADMLP:
Intercept the first 2 characters
[email protected] ~]# head-5/etc/passwd |cut-c-2 ROBIDAADLP
Intercept 5th character start to end
[email protected] ~]# head-5/etc/passwd |cut-c5- : x: 0:0: root:/root:/bin/bashx:1:1: bin:/bin:/sbin/nologinon:x: 2:2:d aemon:/sbin:/sbin/nologinx:3:4: adm:/var /adm:/sbin/nologin:4:7: lp:/var/spool/lpd:/sbin/nologin
The Cut command for Linux