cut

cut 功能说明:cut命令从文件的每一行剪切字节、字符或字段,并将这些字节、字符或字段输出至标准输出。

参数选项:
-b  以字节为单位进行分割。
-n  取消分割多字节字符,与选项-b一起使用。
-c  以字符为单位进行分割。
-d  自定义分割符,默认以tab为分割符。
-f  与选项-d一起使用,指定显示哪个区域。
N  第N个字节、字符或字段。
N-  从第N个字节、字符或字段开始直至行尾。
N-M  从第N到第M(含第M)个字节、字符或字段。
-M  从第1到M(含第M)个字节、字符或字段。

 
以字节为分割符范例
[root@testdb62 ~]# cat a.log
I am cutlog mynum is 88888888
[root@testdb62 ~]# cut -b 3 a.log
a
[root@testdb62 ~]# cut -b 3-5,10 a.log
am o
[root@testdb62 ~]# cut -b -3 a.log
I a
[root@testdb62 ~]# cut -b 3- a.log
am cutlog mynum is 88888888
[root@testdb62 ~]# cut -b -3,3- a.log
I am cutlog mynum is 88888888
 

以字符为分割符
说明:用选项-c则会以字符为单位,输出正常。而选项-b只会傻傻第以字节(8位二进制位)来计算,输出就是乱码。当遇到多字节字符时,可以使用-n选项,-n用于告诉cut不要以多字节字符拆开。
[root@testdb62 ~]# cat a.log
I am cutlog mynum is 88888888
[root@testdb62 ~]# cut -c 2-10 a.log
 am cutlo
[root@testdb62 ~]# cut -b 2-10 a.log
 am cutlo
[root@testdb62 ~]# cat a.log
I am cutlog mynum is 88888888
星期一上班
[root@testdb62 ~]# cut -c 2-10 a.log
 am cutlo
期一上班
[root@testdb62 ~]# cut -b 2-10 a.log
 am cutlo
期一
[root@testdb62 ~]# cut -nb 2-10 a.log
 am cutlo
期一上班
 

自定义分割符例子
[root@testdb62 ~]# head -n 5 /etc/passwd |  cut -d : -f 1
root
bin
daemon
adm
lp
[root@testdb62 ~]# head -n 5 /etc/passwd |  cut -d : -f 3-5
0:0:root
1:1:bin
2:2:daemon
3:4:adm
4:7:lp
原文地址:https://www.cnblogs.com/l10n/p/9416565.html