shell工具-cut

cut

cut的工作就是“剪”,具体说就是在文件中负责剪切数据用的。cut命令从文件的每一行剪切字节、字符、和字段并将这些字节、字符和字段输出

基本用法

cut [参数] filename
# 说明:默认分割符是制表符

参数说明

选项参数功能
-f 列号,提取第几列
-d 分隔符,按照指定分隔符分割列

 

 

 

 

案例实操

数据准备

[root@slave2 testshell]# touch cut.txt
[root@slave2 testshell]# vim cut.txt 
dong shen
guan zhen
wo wo
lai lai
le le

切割cut.txt第一列

[root@slave2 testshell]# cut -d " " -f 1 cut.txt 
dong
guan
wo
lai
le

切割cut.txt第二、三列

[root@slave2 testshell]# vim cut.txt 
dong shen si
guan zhen chuan
wo wo wo
lai lai lai
le le le
[root@slave2 testshell]# cut -d " " -f 2,3 cut.txt   
shen si
zhen chuan
wo wo
lai lai
le le

在cut.txt文件中切割出guan

[root@slave2 testshell]# cat cut.txt | grep ^guan
guan zhen chuan
[root@slave2 testshell]# cat cut.txt | grep ^guan | cut -d " " -f 1
guan

选取系统PATH变量值,第二个“:”开始后的所有路径

[root@slave2 testshell]# echo $PATH
/usr/local/src/kafka_2.11-0.10.2.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/src/scala-2.11.0/bin:/usr/local/src/spark-2.1.2-bin-hadoop2.6/bin:/usr/local/src/jdk1.8.0_171/bin:/usr/local/src/hadoop-2.6.1/bin:/usr/local/src/zookeeper-3.4.10/bin:/root/bin
[root@slave2 testshell]# echo $PATH | cut -d : -f 3-
/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/src/scala-2.11.0/bin:/usr/local/src/spark-2.1.2-bin-hadoop2.6/bin:/usr/local/src/jdk1.8.0_171/bin:/usr/local/src/hadoop-2.6.1/bin:/usr/local/src/zookeeper-3.4.10/bin:/root/bin

切割ipconfig后打印的IP地址

[root@slave2 testshell]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.242.22  netmask 255.255.255.0  broadcast 192.168.242.255
        inet6 fe80::c2d4:1290:9c7c:ca0b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:85:af:c5  txqueuelen 1000  (Ethernet)
        RX packets 2130943  bytes 2041310938 (1.9 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1429126  bytes 295887628 (282.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1566924  bytes 286612476 (273.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1566924  bytes 286612476 (273.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@slave2 testshell]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.242.22  netmask 255.255.255.0  broadcast 192.168.242.255
        inet6 fe80::c2d4:1290:9c7c:ca0b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:85:af:c5  txqueuelen 1000  (Ethernet)
        RX packets 2131292  bytes 2041345171 (1.9 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1429884  bytes 295953700 (282.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@slave2 testshell]# ifconfig ens33 | grep "inet "
        inet 192.168.242.22  netmask 255.255.255.0  broadcast 192.168.242.255
[root@slave2 testshell]# ifconfig ens33 | grep "inet " | cut -d " " -f 10
192.168.242.2
posted on 2019-02-22 20:15  0x153_小波  阅读(230)  评论(0编辑  收藏  举报