下面的命令环境为centos6.6系统;
grep+cut方法:
[root@localhost ~]# ifconfig eth0|grep 'inet addr:'|cut -d: -f2|cut -d " " -f1
192.168.20.50
sed方法:使用正则进行替换
[root@localhost ~]# ifconfig eth0|sed -nr '2s#^.*addr:(.*) Bca.*$#\1#g'p192.168.20.50
sed方法(不太好理解)
匹配/inet /这一行 进行2次替换,替换为空,剩下的就是ip地址;
[root@localhost ~]# ifconfig eth0|sed -n '/inet /{s/.*addr://;s/ .*//;p}'
192.168.20.50
awk方法:
[root@localhost ~]# ifconfig eth0|awk '/inet addr:/ {print $2}'|awk -F: '{print $2}'
192.168.20.50
awk 一条命令方法:(根据实际环境,查看ip所在行进行调整)
[root@localhost ~]# ip add|awk -F '[ /]+' 'NR==8 {print $3}'
192.168.20.50