shell工具-cut

时间:2023-03-09 14:35:32
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  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 , 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
guan

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

[root@slave2 testshell]# echo $PATH
/usr/local/src/kafka_2.-0.10.2.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/src/scala-2.11./bin:/usr/local/src/spark-2.1.-bin-hadoop2./bin:/usr/local/src/jdk1..0_171/bin:/usr/local/src/hadoop-2.6./bin:/usr/local/src/zookeeper-3.4./bin:/root/bin
[root@slave2 testshell]# echo $PATH | cut -d : -f -
/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/src/scala-2.11./bin:/usr/local/src/spark-2.1.-bin-hadoop2./bin:/usr/local/src/jdk1..0_171/bin:/usr/local/src/hadoop-2.6./bin:/usr/local/src/zookeeper-3.4./bin:/root/bin

切割ipconfig后打印的IP地址

[root@slave2 testshell]# ifconfig
ens33: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.242.22 netmask 255.255.255.0 broadcast 192.168.242.255
inet6 fe80::c2d4::9c7c:ca0b prefixlen scopeid 0x20<link>
ether :0c:::af:c5 txqueuelen (Ethernet)
RX packets bytes (1.9 GiB)
RX errors dropped overruns frame
TX packets bytes (282.1 MiB)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
inet6 :: prefixlen scopeid 0x10<host>
loop txqueuelen (Local Loopback)
RX packets bytes (273.3 MiB)
RX errors dropped overruns frame
TX packets bytes (273.3 MiB)
TX errors dropped overruns carrier collisions
[root@slave2 testshell]# ifconfig ens33
ens33: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.242.22 netmask 255.255.255.0 broadcast 192.168.242.255
inet6 fe80::c2d4::9c7c:ca0b prefixlen scopeid 0x20<link>
ether :0c:::af:c5 txqueuelen (Ethernet)
RX packets bytes (1.9 GiB)
RX errors dropped overruns frame
TX packets bytes (282.2 MiB)
TX errors dropped overruns carrier collisions [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
192.168.242.2