linux常用命令之cut

今天玩一下cut命令

cut命令 用于按列提取字符

首先先制作一个文件,从/etc/passwd里面提取前十行内容并把它重定向到test1.txt
效果如下:
其中的数字是本人添加,便于查看结果
示例一:

[root@linuxprobe /]# head -n 10 /etc/passwd > test1.txt
[root@linuxprobe /]# vim test1.txt
[root@linuxprobe /]# cat test1.txt
123456789123456789123456789123456789123456789
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

先用一下-b参数吧
-b 参数可以按字节来查看文件中的内容
提取第一列
示例二

[root@linuxprobe /]# cut -b 1 test1.txt
1
r
b
d
a
l
s
s
h
m
o

提取第一到第五列
示例三

[root@linuxprobe /]# cut  -b -5 test1.txt
12345
root:
bin:x
daemo
adm:x
lp:x:
sync:
shutd
halt:
mail:
opera

提取1 3 5 7 列,顺序故意写错,结果按从小到大显示
示例四

[root@linuxprobe /]# cut -b 1,5,3,7 test1.txt
1357
ro::
bnx1
deo:
amx3
l:::
sn::
sudw
hl::
mi::
oeao

从第四列到最后一列
示例五

[root@linuxprobe /]# cut -b 4- test1.txt
456789123456789123456789123456789123456789
t:x:0:0:root:/root:/bin/bash
:x:1:1:bin:/bin:/sbin/nologin
mon:x:2:2:daemon:/sbin:/sbin/nologin
:x:3:4:adm:/var/adm:/sbin/nologin
x:4:7:lp:/var/spool/lpd:/sbin/nologin
c:x:5:0:sync:/sbin:/bin/sync
tdown:x:6:0:shutdown:/sbin:/sbin/shutdown
t:x:7:0:halt:/sbin:/sbin/halt
l:x:8:12:mail:/var/spool/mail:/sbin/nologin
rator:x:11:0:operator:/root:/sbin/nologin

再来玩玩-d 和 -f 参数吧
-f参数是按照字段选取
-d参数用于设置间隔符号,-f参数用于设置需要看的列数
-d与-f参数要一块使用
下面这段是用于提取以冒号为间隔符的 第六个冒号前面那一列
示例六

[root@linuxprobe /]# cut -d: -f 6 test1.txt
123456789123456789123456789123456789123456789
/root
/bin
/sbin
/var/adm
/var/spool/lpd
/sbin
/sbin
/sbin
/var/spool/mail
/root

-s表示不包括那些不含分隔符的行(这样有利于去掉注释和标题)
只显示含有TAB的列
看了大神的博客说的好像是会计软件输出的文件正好这个命令参数适用。改天尝试一下!
创建一个文本文件,第一行为回车键。
示例七

[root@linuxprobe /]# vim test.txt
[root@linuxprobe /]# cat test.txt
1
2	linux		就该这么学	第二行TAB键
3	linux		就该这么学	第三行TAB键
4   linux       就该这么学    第四行空格键
5	linux		就该这么学	第五行TAB建


用-b参数与-s参数组合,报错提示本参数只与字段合作 即 -f参数
示例八

[root@linuxprobe /]# cut -b 1- -s test.txt
cut: suppressing non-delimited lines makes sense
	only when operating on fields
Try 'cut --help' for more information.

输出将忽略不含有TAB键的行,即以回车键结束,以空格键间隔的行被忽略。
首先输出是剔除不合条件的行,然后再输出你所要求的列。
示例九

[root@linuxprobe /]# cut -f 1- -s test.txt
2	linux	就该这么学	第二行TAB键
3	linux	就该这么学	第三行TAB键
5	linux	就该这么学	第五行TAB建
[root@linuxprobe /]# cut -f 2- -s test.txt
linux	就该这么学	第二行TAB键
linux	就该这么学	第三行TAB键
linux	就该这么学	第五行TAB建
[root@linuxprobe /]# cut -f 3- -s test.txt
就该这么学	第二行TAB键
就该这么学	第三行TAB键
就该这么学	第五行TAB建
[root@linuxprobe /]# cut -f 4- -s test.txt
第二行TAB键
第三行TAB键
第五行TAB建

-c参数是按字符提取,多用于汉字。用在英文上跟-b没有什么区别。
重新编辑一下test.txt文件如下所示
示例十

[root@linuxprobe /]# vim test.txt
[root@linuxprobe /]# cat test.txt
12345
linux就该这么学1
linux就该这么学2
linux就该这么学3
linux就该这么学4
linux就该这么学5
linux就该这么学6

一个英文是一个字节。用在英文上-c 与 -b效果是等同的。
下面的例子中是提取test.txt文件中第二列内容
示例十一

[root@linuxprobe /]# cut -b 2 test.txt
2
i
i
i
i
i
i
[root@linuxprobe /]# cut -c 2 test.txt
2
i
i
i
i
i
i

一个汉字是三个字节(好像是在UTF-8编码中一个汉字为三个字节)
下面的例子是提取test.txt文件中第六个字符(第六列),先说正确的吧
示例十二

[root@linuxprobe /]# cut -c 6 test.txt

就
就
就
就
就
就

也可用-nb参数
-n :取消分割多字节字符。仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的范围之内,该字符将被写出;否则,该字符将被排除。
示例十三

[root@linuxprobe /]# cut -nb 6 test.txt

就
就
就
就
就
就

如果只用 -b 参数的话英文就是第几个就是第几个,一个汉字是三个字节。要显示上文效果应该如下
示例十四

[root@linuxprobe /]# cut -b 6-8 test.txt

就
就
就
就
就
就

否则就
示例十五

[root@linuxprobe /]# cut -b 8 test.txt

�
�
�
�
�
�

最后总结一下学习结果
比如要提取英文 (i) 和汉字(学 )还有 最后一列的数字。再把结果重定向到test2.txt(已存在)
(技术不好不会用正则表达式,以后学会了再搞点高级的。嘿嘿)
示例十六

[root@linuxprobe /]# cut -b 2,18-22 test.txt > test2.txt | cat test2.txt
2
i学1
i学2
i学3
i学4
i学5
i学6

[root@linuxprobe /]# cut -c 2,10,11 test.txt > test2.txt | cat test2.txt
2
i学1
i学2
i学3
i学4
i学5
i学6
[root@linuxprobe /]# cut -nb 2,10-11 test.txt > test2.txt | cat test2.txt
2
i学1
i学2
i学3
i学4
i学5
i学6


参考前辈的文章 Linux下cut命令用法详解
终于写完了
最后愿我们共同成长吧!!

  • 14
    点赞
  • 97
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值