Linux文本处理工具cut(截切数据)

Linux文本处理工具cut(剪切数据)

     cut在文件中负责剪切数据用的。cut命令从文件每一行剪切字节、字符、和字段并将这些字节、字符和字段输出。
  1. 基本用法
    cut [选项参数] filename
    说明:默认分隔符是制表符
  2. 选项参数说明
    -f::列号,提取第几列
    -d:分隔符,按照指定分隔符分割列
    -b:以字节为单位进行分隔
    -c:以字符的单位取出,固定字符区间

1. 数据文件cut.txt

cut.txt
1 张三 男 28
2 李四 男 31
3 Lily 女 32
4 Lucy 女 29
5 Jack 男 27

[root@localhost jiaoben]# touch cut.txt
[root@localhost jiaoben]# vim cut.txt
[root@localhost jiaoben]# cat cut.txt
1 张三 男 28
2 李四 男 31
3 Lily 女 32
4 Lucy 女 29
5 Jack 男 27

1. 取cut.txt第一列

cut -d " " -f 1 cut.txt

[root@localhost jiaoben]# cut -d " " -f 1 cut.txt
1
2
3
4
5

2. 取cut.txt第一列和第二列

cut -d " " -f 1,2 cut.txt

[root@localhost jiaoben]# cut -d " " -f 1,2 cut.txt
1 张三
2 李四
3 Lily
4 Lucy
5 Jack

3. 取cut.tx第二列到第四列

cut -d " " -f 2- cut.txt
cut -d " " -f 2-4 cut.txt

[root@localhost jiaoben]# cut -d " " -f 2- cut.txt
张三 男 28
李四 男 31
Lily 女 32
Lucy 女 29
Jack 男 27

[root@localhost jiaoben]# cut -d " " -f 2-4 cut.txt
张三 男 28
李四 男 31
Lily 女 32
Lucy 女 29
Jack 男 27

4. 取cut.txt前三列

cut -d " " -f -3 cut.txt
cut -d " " -f 1-3 cut.txt

[root@localhost jiaoben]# cut -d " " -f -3 cut.txt
1 张三 男
2 李四 男
3 Lily 女
4 Lucy 女
5 Jack 男

[root@localhost jiaoben]# cut -d " " -f 1-3 cut.txt
1 张三 男
2 李四 男
3 Lily 女
4 Lucy 女
5 Jack 男

5. 取cut.txt的前三个字符

cut -c 1-3 cut.txt

[root@localhost jiaoben]# cut -c 1-3 cut.txt
123 L
4 L
5 J

6. 在cut.txt文件中切割出Lily

cat cut.txt | grep “Lily” | cut -d " " -f 2

[root@localhost jiaoben]# cat cut.txt | grep "Lily" | cut -d " " -f 2
Lily

7. 取出系统PATH变量值,第2个“:”开始后的所有路径

echo $PATH | cut -d : -f 2-

[root@localhost jiaoben]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost jiaoben]# echo $PATH | cut -d : -f 2-
/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

8. 取ifconfig后打印的IP地址

ifconfig ens33 | grep “netmask” | cut -d " " -f 10

[root@localhost jiaoben]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.111.15  netmask 255.255.255.0  broadcast 192.168.111.255
        inet6 fe80::709d:adbe:cb11:42ef  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:89:a9:62  txqueuelen 1000  (Ethernet)
        RX packets 14643  bytes 1236939 (1.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8325  bytes 883703 (862.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost jiaoben]# ifconfig ens33 | grep "inet"
        inet 192.168.111.15  netmask 255.255.255.0  broadcast 192.168.111.255
        inet6 fe80::709d:adbe:cb11:42ef  prefixlen 64  scopeid 0x20<link>
[root@localhost jiaoben]# ifconfig ens33 | grep "inet" | cut -d " " -f 10
192.168.111.15
fe80::709d:adbe:cb11:42ef
[root@localhost jiaoben]# ifconfig ens33 | grep "netmask" | cut -d " " -f 10
192.168.111.15
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值