cut命令

cut主要是用来分割文件中的字符串,并且根据要求显示。 测试文件为/etc/passwd___

# cat /etc/passwd___ 
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh

-b选项可以按照字节截取,-c和-b的区别在于,-c按照字符截取,在处理中文时方便

# cut -b 1 /etc/passwd___
r
d
b
s
s
g
# cut -b 1-4 /etc/passwd___
root
daem
bin:
sys:
sync
game
# cut -b 1-4,10-15 /etc/passwd___
root0:root
daem1:1:da
bin::bin:/
sys::sys:/
sync65534:
game:60:ga
# cut -b -4 /etc/passwd___
root
daem
bin:
sys:
sync
game
# cut -b 4- /etc/passwd___
t:x:0:0:root:/root:/bin/bash
mon:x:1:1:daemon:/usr/sbin:/bin/sh
:x:2:2:bin:/bin:/bin/sh
:x:3:3:sys:/dev:/bin/sh
c:x:4:65534:sync:/bin:/bin/sync
es:x:5:60:games:/usr/games:/bin/sh
-d用来确定分隔符,-f用来取出字段
# cut -f 1 /etc/passwd___
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
# cut -d : -f 1 /etc/passwd___
root
daemon
bin
sys
sync
games
# cut -d : -f 1,3 /etc/passwd___
root:0
daemon:1
bin:2
sys:3
sync:4
games:5

-s用来控制输出

# cut -d : -f 1-4 -s --output-delimiter="|" /etc/passwd___
root|x|0|0
daemon|x|1|1
bin|x|2|2
sys|x|3|3
sync|x|4|65534
games|x|5|60

    参考:http://roclinux.cn/?p=1328http://blog.51yip.com/linux/1077.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注