Linux用grep和awk命令快速过滤出ip地址

命令:ip a|grep "global" | awk -F "[ /]+" '{print $3}' 

推导出此命令的过程

1、查看ip a 的标准输出

[root@CentOS-01:~]$ ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:24:f7:67 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.135/24 brd 192.168.197.255 scope global noprefixroute dynamic ens33
       valid_lft 1094sec preferred_lft 1094sec
    inet6 fe80::8382:76f8:171e:86fd/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

2、用grep过滤出ip地址所在行

[root@CentOS-01:~]$ ip a|grep "global"
    inet 192.168.197.135/24 brd 192.168.197.255 scope global noprefixroute dynamic ens33

3、用awk取出ip地址

[root@CentOS-01:~]$ ip a|grep "global" | awk -F "[ /]+" '{print $3}' 
192.168.197.135

你可能感兴趣的:(linux,tcp/ip,运维)