文件内容处理命令《cut》

cut

作用:从文件的每一行获取指定内容输出到标准屏幕

语法:

cut [-bn][参数][文件名]
或者
cut [-c][参数][文件名]
或者
cut[-s][-d][参数][-f][参数][文件名] 

选项:

-b, --bytes=LIST    以字节为单位,输出指定位置( 位置从1开始编号)的字节。无法解析中文。
-n     仅配合-b选项使用,不拆分多字节字符,这样就可以识别中文(相当于-c选项)。

-c, --characters=LIST    以字符为单位,输出指定位置( 位置从1开始编号)的字符。

-f, --fields=LIST   提取指定列的字段(默认以tab作为分隔符),并且同时输出不包含分隔符的行,除非制定了-s选项。
-d, --delimiter=DELIM    以指定分割符,配合-f选项。
-s, --only-delimited     不打印不包含分隔符的行

-b, -c,-f后面跟的参数代表提取内容的列号,可以是一个范围,或者是由逗号隔开的多列。n或者n-m或者n-或者-m或者n,m

实例:

-b

[root@VM_0_6_centos tiger]# cat test.text
id      name    sex     age     clazz_id
1       jack    男      23      1
2       rose    女      18      1
3       tom     男      21      1
4       alice   女      20      1
[root@VM_0_6_centos tiger]# cut -b 1 test.text
i
1
2
3
4
[root@VM_0_6_centos tiger]# cut -b 2 test.text  
d




[root@VM_0_6_centos tiger]# cut -b 3 test.text  

j
r
t
a
[root@VM_0_6_centos tiger]# cut -b 1,2,3 test.text
id
1       j
2       r
3       t
4       a
-c
[root@VM_0_6_centos tiger]# cat test.text
id      name    sex     age     clazz_id
1       jack    男      23      1
2       rose    女      18      1
3       tom     男      21      1
4       alice   女      20      1
[root@VM_0_6_centos tiger]# cut -c 8 test.text

男
女


[root@VM_0_6_centos tiger]# cut -c 1,8 test.text
i
1男
2女
3
4

-d

如果以空格作为分割符的话,是不会把tab拆分为空格的,并且也不会把连续的几个空格识别为一个,就会导致出现的第一个空格是分隔符,紧跟着的空格识别为一列

[root@VM_0_6_centos tiger]# cat test.text
id      name    sex     age     clazz_id
1       jack    男      23      1
2       rose    女      18      1
3       tom     男      21      1
4       alice   女      20      1
adfvbsdfgvbsfdgbsf
我们巨坑高考加油好几个
daca         sdf     adfa       
adsf        adf     adsfv
dasf     dsf dsafgsfd
[root@VM_0_6_centos tiger]# cut -d " " -f 1,2 test.text
id      name    sex     age     clazz_id
1       jack    男      23      1
2       rose    女      18      1
3       tom     男      21      1
4       alice   女      20      1
adfvbsdfgvbsfdgbsf
我们巨坑高考加油好几个
daca
adsf
dasf
[root@VM_0_6_centos tiger]# cut -d " " -s -f 1,2 test.text  
daca
adsf
dasf
[root@VM_0_6_centos tiger]# cut -d ":" -f 1,2 /etc/passwd
root:x
。。。。。。
tanzhitong:x

-f

[root@VM_0_6_centos tiger]# cat test.text
id      name    sex     age     clazz_id
1       jack    男      23      1
2       rose    女      18      1
3       tom     男      21      1
4       alice   女      20      1
adfvbsdfgvbsfdgbsf
我们巨坑高考加油好几个
[root@VM_0_6_centos tiger]# cut -f 1 test.text
id
1
2
3
4
adfvbsdfgvbsfdgbsf
我们巨坑高考加油好几个
[root@VM_0_6_centos tiger]# cut -s -f 3 test.text   
sex
男
女
男
女
[root@VM_0_6_centos tiger]# cut -f 1,3 test.text
id      sex
1       男
2       女
3       男
4       女
adfvbsdfgvbsfdgbsf
我们巨坑高考加油好几个

-n

[root@VM_0_6_centos tiger]# cat student.txt
id      name    sex     age     clazz_id
1       jack    男      23      1
2       rose    女      18      1
3       tom     男      21      1
4       alice   女      20      1
[root@VM_0_6_centos tiger]# cut -b 6,7,8,9 student.txt    
me      s
k       e           男
ce      root@VM_0_6_centos tiger]# cut -n -b 6,7,8,9 student.txt
me      s
k       男
e       女
        男      2
ce      女
[root@VM_0_6_centos tiger]#
[root@VM_0_6_centos tiger]# cat student.txt
id      name    sex     age     clazz_id
1       jack    男      23      1
2       rose    女      18      1
3       tom     男      21      1
4       alice   女      20      1
[root@VM_0_6_centos tiger]# cut -b 6-9 student.txt
me      s
k       e           男
ce      root@VM_0_6_centos tiger]# cut -c 6-9 student.txt  
me      s
k       男
e       女
        男      2
ce      女
[root@VM_0_6_centos tiger]# cut -f 2-3 student.txt     
name    sex
jack    男
rose    女
tom     男
alice   女

查询当前系统中的普通用户名

[root@VM_0_6_centos ~]# cat /etc/passwd | grep /bin/bash | grep  -v root | cut -d ":" -f 1
mysql
tiger
super
tigeryh
tanzhitong
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值