shell之cut

shell之cut

以每行数据为单位,对数据进行切割;类似于sed;

切割时的分隔符
-d用来指定分隔符;默认以tab为分隔符;
当指定以空格为分割符时,可以这样

cut -d ' ' -f 1

直接用单引号或者双引号将空格包裹起来
注意!注意!注意!-d的参数不能包含多个空格

[root@localhost ~]# cat test.txt |cut -d '  ' -f 1
cut: the delimiter must be a single character
Try 'cut --help' for more information.
[root@localhost ~]#

cut 的定位依据
所谓“定位依据”,通俗地讲就是,我该如何告诉 cut 我想定位到哪一段内容进行剪切呢?

其实,cut 命令共接受三类定位方法:
第一,按字节(bytes)定位,用-b选项。
第二,按字符(characters)定位,用-c选项。
第三,按域(fields)定位,用-f选项。

[root@localhost ~]# 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@localhost ~]# cat /etc/passwd|head -n 3|cut -d: -f 5-6
root:/root
bin:/bin
daemon:/sbin
[root@localhost ~]#

识别tab切割符和空格分隔符

[root@localhost ~]# cat test.txt
start shell command     of cut
[root@localhost ~]# sed -n l test.txt
start shell command\tof cut$
[root@localhost ~]# cat test.txt |cut -f 1-2
start shell command     of cut
[root@localhost ~]# cat test.txt |cut -f 1
start shell command
[root@localhost ~]# cat test.txt |cut -f 2
of cut
[root@localhost ~]#

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值