从0开始学习shell--文本处理工具cut

文本小工具--cut
列处理

-c 以字符为单位进行分割截取
-d 自定义分隔符,默认为制表符\t
-f 与-d一起使用,指定截取几个区域

截取passwd文件中的第一列和第七列,以:为分隔符
[root@localhost tmp]# cut -d':' -f1,7 passwd | head
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin
adm:/sbin/nologin
lp:/sbin/nologin
sync:/bin/sync
shutdown:/sbin/shutdown
halt:/sbin/halt
mail:/sbin/nologin
operator:/sbin/nologin

Cut -c 以字符进行截取
[root@localhost tmp]# cut -c1-5 passwd | head
root:
bin:x
daemo
adm:x
lp:x:
sync:
shutd
halt:
mail:
opera
[root@localhost tmp]# cut -c10- passwd | head
0:root:/root:/bin/bash
:bin:/bin:/sbin/nologin
2:2:daemon:/sbin:/sbin/nologin
:adm:/var/adm:/sbin/nologin
lp:/var/spool/lpd:/sbin/nologin
0:sync:/sbin:/bin/sync
x:6:0:shutdown:/sbin:/sbin/shutdown
0:halt:/sbin:/sbin/halt
12:mail:/var/spool/mail:/sbin/nologin
x:11:0:operator:/root:/sbin/nologin

查看当前运行级别
runlevel | cut -c 3
runlevel | cut -d' ' -f1

[root@localhost tmp]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.209.128 netmask 255.255.255.0 broadcast 192.168.209.255
inet6 fe80::20c:29ff:fe1d:8d4b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1d:8d:4b txqueuelen 1000 (Ethernet)
RX packets 3479 bytes 309435 (302.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1770 bytes 261542 (255.4 KiB)
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 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:05:55:7f txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@localhost tmp]# ifconfig eth0 | grep broadcast | cut -c 14-28
192.168.209.128
[root@localhost tmp]#

截取IP,子网掩码和广播地址
[root@localhost tmp]# ifconfig eth0 | grep broadcast | tr -d 'a-zA-Z' | tr ' ' '\n' | grep -v '^$'
192.168.209.128
255.255.255.0
192.168.209.255
[root@localhost tmp]#

[root@localhost tmp]# ifconfig eth0 | grep ether | cut -d ' ' -f10
00:0c:29:1d:8d:4b
[root@localhost tmp]# ifconfig eth0 | grep ether | tr -s ' '
ether 00:0c:29:1d:8d:4b txqueuelen 1000 (Ethernet)
[root@localhost tmp]# ifconfig eth0 | grep ether | tr -s ' ' | cut -d' ' -f 2
ether
[root@localhost tmp]# ifconfig eth0 | grep ether | tr -s ' ' | cut -d' ' -f 4
txqueuelen
[root@localhost tmp]# ifconfig eth0 | grep ether | tr -s ' ' | cut -d' ' -f 3
00:0c:29:1d:8d:4b

Tr -s 压缩多余的空格

上一篇:【linux】循序渐进学运维-基础篇-操作系统初始化


下一篇:awk基础应用