5,cut 命令

cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。

cut –b 剪切单个字节。
[root@centos7 ~]# cat aa.txt 
111
222
333
444
555
[root@centos7 ~]# 
[root@centos7 ~]# cut -b 1 aa.txt   #剪切单个字节。
1
2
3
4
5
[root@centos7 ~]# cut -b 2,3 aa.txt #剪切多个字节。
11
22
33
44
55
[root@centos7 ~]# 


-b(bytes) :以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。 
-c(characters) :以字符为单位进行分割。 
-d :自定义分隔符,默认为制表符。 
-f(filed) :与-d一起使用,指定显示哪个区域。 
-n :取消分割多字节字符。仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的
范围之内,该字符将被写出;否则,该字符将被排除。


[root@centos7 ~]# cat bb.txt 
星期1
星期2
星期3
[root@centos7 ~]# 

[root@centos7 ~]# cut -b 2 bb.txt  #出现乱码,因为-b是针对字节裁剪的,如果想剪切字符,那么就需要用-n ,取消分割多字节字符。
@
#
@
[root@centos7 ~]# 

[root@centos7 ~]# cut -nb 2 bb.txt 
期
期
期
[root@centos7 ~]# 


-c 剪切字符
[root@centos7 ~]# cat bb.txt 
星期1
星期2
星期3
[root@centos7 ~]# 
[root@centos7 ~]# cut -c 1 bb.txt 
星
星
星
[root@centos7 ~]# cut -c 2 bb.txt 
期
期
期
[root@centos7 ~]# 


[root@centos7 ~]# 
[root@centos7 ~]# cat /etc/passwd|head -n 3
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# cat /etc/passwd|head -n 3|cut -d : -f 1
root
bin
daemon
[root@centos7 ~]# 

 

posted @ 2020-10-18 22:47  pwcc  阅读(156)  评论(0)    收藏  举报