cut 命令

 

cut

-d 自定义分隔符,默认为制表符

-f 与-d一起使用,指定显示哪个区域

-

 

提取列或字段 - cut
cut用于显示文件或者标准输入的特定列,如:
cut常用的选项有:
-b [范围] :仅显示行中指定直接范围的内容
-c[范围] :仅显示行中指定范围的字符
-d:指定字段的分隔符,默认的字段分隔符为“TAB”
-f [范围]:显示指定第num 个字段的内容,可以用逗号隔开显示多个字段

 

 cat /etc/passwd
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
saslauth:x:998:76:Saslauthd user:/run/saslauthd:/sbin/nologin
unbound:x:997:996:Unbound DNS resolver:/etc/unbound:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:D-Bus:/var/run/dbus:/sbin/nologin
polkitd:x:996:994:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
chrony:x:995:992::/var/lib/chrony:/sbin/nologin
systemd-network:x:990:990:systemd Network Management:/:/usr/sbin/nologin
systemd-resolve:x:989:989:systemd Resolver:/:/usr/sbin/nologin
systemd-timesync:x:988:988:systemd Time Synchronization:/:/usr/sbin/nologin

// 显示这个文件中每一行的第三个字节
cut -b 3 /etc/passwd
o
n
e
m
:
n
u
l
i
e
m
p
b
s
s
b
c
h
u
l
s
r
s
s
s

 

//可以显示每一行的第1-3个字符
cut -c 1-3 /etc/passwd
roo
bin
dae
adm
lp:
syn
shu
hal
mai
ope
gam
ftp
nob
sys
sas
unb
dhc
ssh
dbu
pol
tss
chr
sys
sys
sys

 

cut -d: 指定分隔符为: 然然用-f 1-3 显示以:分割的第1-3字段

cut -d: -f 1-3 /etc/passwd
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
operator:x:11
games:x:12
ftp:x:14
nobody:x:65534
systemd-coredump:x:999
saslauth:x:998
unbound:x:997
dhcpd:x:177
sshd:x:74
dbus:x:81
polkitd:x:996
tss:x:59
chrony:x:995
systemd-network:x:990
systemd-resolve:x:989
systemd-timesync:x:988

 

 

 

[root@salt-server-192 sh]# cat /etc/passwd
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin

 

 

 

[root@localhost sh]# cut -d: -f 3 /etc/passwd 
0
1
2
3
4
5
6
7
8
10
11
12
13
14
99
69
499
89
74
38

// 以冒号为分隔符,取第三段的字符

 

[root@localhost sh]# cat /etc/passwd | cut -d : -f 5
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
FTP User
Nobody
virtual console memory owner
Saslauthd user

Privilege-separated SSH

// 以冒号为分隔符,取第五段的字符

 

 

[root@localhost sh]# cat /etc/passwd | cut -d : -f 3,4,5  
0:0:root
1:1:bin
2:2:daemon
3:4:adm
4:7:lp
5:0:sync
6:0:shutdown
7:0:halt
8:12:mail
10:14:uucp
11:0:operator
12:100:games
13:30:gopher
14:50:FTP User
99:99:Nobody
69:69:virtual console memory owner
499:76:Saslauthd user
89:89:
74:74:Privilege-separated SSH
38:38:

[root@localhost sh]# cat /etc/passwd | cut -d : -f 3-5  
0:0:root
1:1:bin
2:2:daemon
3:4:adm
4:7:lp
5:0:sync
6:0:shutdown
7:0:halt
8:12:mail
10:14:uucp
11:0:operator
12:100:games
13:30:gopher
14:50:FTP User
99:99:Nobody
69:69:virtual console memory owner
499:76:Saslauthd user
89:89:
74:74:Privilege-separated SSH
38:38:

//以冒号为分隔符,取每一行第3-5段的字符

 

-c 按字符取

[root@localhost sh]# cut -c 1 /etc/passwd 
r
b
d
a
l
s
s
h
m
u
o
g
g
f
n
v
s
p
s
n

//取出每一行的第一个字符

 

 

取出每一行第10个字符到每行结束

[root@localhost sh]# cut -c 10- /etc/passwd
0:root:/root:/bin/bash
:bin:/bin:/sbin/nologin
2:2:daemon:/sbin:/sbin/nologin
:adm:/var/adm:/sbin/nologin
lp:/var/spool/lpd:/sbin/nologin
0:sync:/sbin:/bin/sync
x:6:0:shutdown:/sbin:/sbin/shutdown
0:halt:/sbin:/sbin/halt
12:mail:/var/spool/mail:/sbin/nologin
:14:uucp:/var/spool/uucp:/sbin/nologin
x:11:0:operator:/root:/sbin/nologin
2:100:games:/usr/games:/sbin/nologin
13:30:gopher:/var/gopher:/sbin/nologin
50:FTP User:/var/ftp:/sbin/nologin
99:99:Nobody:/:/sbin/nologin
:69:virtual console memory owner:/dev:/sbin/nologin
x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
:89:89::/var/spool/postfix:/sbin/nologin
:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
38::/etc/ntp:/sbin/nologin
[root@localhost sh]# cut -c 5- /etc/passwd  
:x:0:0:root:/root:/bin/bash
x:1:1:bin:/bin:/sbin/nologin
on:x:2:2:daemon:/sbin:/sbin/nologin
x:3:4:adm:/var/adm:/sbin/nologin
:4:7:lp:/var/spool/lpd:/sbin/nologin
:x:5:0:sync:/sbin:/bin/sync
down:x:6:0:shutdown:/sbin:/sbin/shutdown
:x:7:0:halt:/sbin:/sbin/halt
:x:8:12:mail:/var/spool/mail:/sbin/nologin
:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
ator:x:11:0:operator:/root:/sbin/nologin
s:x:12:100:games:/usr/games:/sbin/nologin
er:x:13:30:gopher:/var/gopher:/sbin/nologin
x:14:50:FTP User:/var/ftp:/sbin/nologin
dy:x:99:99:Nobody:/:/sbin/nologin
:x:69:69:virtual console memory owner:/dev:/sbin/nologin
auth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
fix:x:89:89::/var/spool/postfix:/sbin/nologin
:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
x:38:38::/etc/ntp:/sbin/nologin
[root@localhost sh]# cut -c 5- /etc/passwd 
:x:0:0:root:/root:/bin/bash
x:1:1:bin:/bin:/sbin/nologin
on:x:2:2:daemon:/sbin:/sbin/nologin
x:3:4:adm:/var/adm:/sbin/nologin
:4:7:lp:/var/spool/lpd:/sbin/nologin
:x:5:0:sync:/sbin:/bin/sync
down:x:6:0:shutdown:/sbin:/sbin/shutdown
:x:7:0:halt:/sbin:/sbin/halt
:x:8:12:mail:/var/spool/mail:/sbin/nologin
:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
ator:x:11:0:operator:/root:/sbin/nologin
s:x:12:100:games:/usr/games:/sbin/nologin
er:x:13:30:gopher:/var/gopher:/sbin/nologin
x:14:50:FTP User:/var/ftp:/sbin/nologin
dy:x:99:99:Nobody:/:/sbin/nologin
:x:69:69:virtual console memory owner:/dev:/sbin/nologin
auth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
fix:x:89:89::/var/spool/postfix:/sbin/nologin
:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
x:38:38::/etc/ntp:/sbin/nologin

 

 取每一行 第一个字符到第5个字符

[root@localhost sh]# cut -c -5 /etc/passwd
root:
bin:x
daemo
adm:x
lp:x:
sync:
shutd
halt:
mail:
uucp:
opera
games
gophe
ftp:x
nobod
vcsa:
sasla
postf
sshd:
ntp:x

[root@localhost sh]# cut -c 1-5 /etc/passwd 
root:
bin:x
daemo
adm:x
lp:x:
sync:
shutd
halt:
mail:
uucp:
opera
games
gophe
ftp:x
nobod
vcsa:
sasla
postf
sshd:
ntp:x

 

取第2行-第4行字符

[root@localhost sh]# cut -c 2-4 /etc/passwd
oot
in:
aem
dm:
p:x
ync
hut
alt
ail
ucp
per
ame
oph
tp:
obo
csa
asl
ost
shd
tp:

 

 

 

posted @ 2017-11-15 16:58  minger_lcm  阅读(266)  评论(0编辑  收藏  举报