Linux Command cut usage
Cut command
Cut [OPTION] ... [FILE] ...
-D DELIMITER: Indicates delimiter
-F Fileds:
#: Section # Fields
#,#[,#] Discrete multiple fields, such as 1,3,6
#-#连续的多个字段, e.g. 1-6
Mixed use 1-3,7
--output-delimiter=string
[[email protected] ~]# cut -d : -f 1 /etc/passwd root bin daemon adm lp sync shutdown halt mail uucp operator games gopher ftp nobody dbus usbmuxd vcsa rtkit avahi-autoipd abrt haldaemon gdm ntp apache saslauth postfix pulse sshd tcpdump hadoop zhangshan
[[email protected] ~]# cut -d : -f 1,3 /etc/passwd root:0 bin:1 daemon:2 adm:3 lp:4 sync:5 shutdown:6 halt:7 mail:8 uucp:10 operator:11 games:12 gopher:13 ftp:14 nobody:99 dbus :81 usbmuxd:113 vcsa:69 rtkit:499 avahi-autoipd:170 abrt:173 haldaemon:68 gdm:42 ntp:38 apache:48 saslauth:498 postfix:89 pulse:497 sshd:74 tcpdump:72 hadoop:500 zhangshan:501
Usage
-B is split in bytes. These byte locations will ignore multibyte character boundaries unless the-n flag is also specified.
-C is split in characters.
-D Custom delimited defaults think of tabs.
-F is used with-D to specify which area to display.
-N cancels the splitting of multibyte characters.
Detailed explanation later
(1)-B usage
Take the third byte of each row
[[email protected] ~]# whoroot tty2 2016-07-07 06:08root tty1 2016-07-07 06:06 (: 0) Root pts/0 2016-07-07 06:07 (: 0.0) root pts/1 2016-07-07 06:08 (192.168.0.110)
[email protected] ~]# who | Cut-b 3oooo
What do I do if I want to extract the 2nd, 3rd, and 4th bytes of 1th?
[Email protected] ~]# Who|cut-b 1-3,4rootrootrootroot
Take the first 3 bytes with-3
[Email protected] ~]# Who|cut-b -3rooroorooroo
After taking 3 bytes with 3-
[[email protected] ~]# who|cut-b 3-ot tty2 2016-07-07 06:08ot tty1 2016-07-07 06:06 (: 0) OT PT s/0 2016-07-07 06:07 (: 0.0) ot pts/1 2016-07-07 06:08 (192.168.0.110)
(2) The use of-C
Take 1th 2nd, 3rd, and 4th bytes here and-B there's no difference.
[Email protected] ~]# who|cut-c 1-3,4rootrootrootroot
So there is no difference between-B and-C If you extract the Chinese difference will look out to see the situation of Chinese extraction
[[email protected] ~]# cat Sb.txt today is Monday I'm going to work. Today is Tuesday I have to go to work. Today is Wednesday I have to go to work. Today is Thursday I have to go to work. Today is Friday I have to go to work. Today is Saturday I don't have to go to work.
[Email protected] ~]# Cut-b 3 sb.txt
[[email protected] ~]# cut-c 3 sb.txt Yes Yes Yes Yes Yes Yes
You see that? with-C will output normal in characters and-B will only be silly to calculate the output is garbled with byte 8 bit bits.
(3)-N usage
When multiple-byte characters are encountered, you can use the-n option-N to tell the cut not to disassemble multibyte characters. Examples are as follows
The following command has no results
[Email protected] ~]# CUT-NB 2 sb.txt
Or
The following command has no results
[email protected] ~]# Cat Sb.txt | CUT-NB 2
With results
[email protected] ~]# Cat Sb.txt | CUT-NB----------this today
(4) The use of-f
The former five elements of/etc/passwd as an example
[Email protected] ~]# 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] ~]# cat/etc/passwd|head-n 5|cut-d:-F 1ROOTBINDAEMONADMLP
You see that? Use-D to set the delimiter as a colon and then use-F to set the first field I want to take.
Another example below
[Email protected] ~]# cat/etc/passwd|head-n 5|cut-d:-F 1-3,7root:x:0:/bin/bashbin:x:1:/sbin/nologindaemon:x:2:/ Sbin/nologinadm:x:3:/sbin/nologinlp:x:4:/sbin/nologin
Take the first 3
[Email protected] ~]# cat/etc/passwd|head-n 5|cut-d:-f-3root:x:0bin:x:1daemon:x:2adm:x:3lp:x:4
(4) Other-custom delimiter-D
If you encounter spaces and tabs, how to distinguish it I feel a bit messy what to do (there is a problem here.) )
[[email protected] ~]# cat Aa.txt A wordthis is a wordthis are a wordthis is a word
[email protected] ~]# cat aa.txt |cut-d "-F 3aaa Worda
Hope that we all point together progress haha.
This article is from the "Liang blog" blog, make sure to keep this source http://7038006.blog.51cto.com/7028006/1812434
Linux Command cut usage