The method is too many, first simple to concise and gradual.
1.
[Email protected] ~]# ifconfig eth0|grep ' inet addr: ' # # #过滤不是IP地址的行
inet addr:192.168.16.100 bcast:192.168.16.255 mask:255.255.255.0
Or
[Email protected] ~]# ifconfig eth0|sed-n ' 2p ' # # #过滤不是IP地址的行
inet addr:192.168.16.100 bcast:192.168.16.255 mask:255.255.255.0
[Email protected] ~]# ifconfig eth0|sed-n ' 2p ' |sed-n ' s#^.*dr:# #gp '
192.168.16.100 bcast:192.168.16.255 mask:255.255.255.0 # # #已经去掉IP地址头部了.
[Email protected] ~]# ifconfig eth0|sed-n ' 2p ' |sed-n ' s#^.*dr:# #gp ' |sed-n ' s#b.*$# #gp '
# # #去掉IP地址尾巴.
192.168.16.100
2, using grep or sed &&cut combination extraction
Ifconfig eth0|grep "inet addr:" |cut-d ":"-f2|cut-d ""-f1
Ifconfig eth0|grep "inet addr:" |cut-f2-d ":" |cut-f1-d ""
Ifconfig eth0|grep "inet addr:" |cut-d:-f2|cut-d ""-f1
Ifconfig eth0|sed-n ' 2p ' |cut-c21-35
3. Use grep or sed &&awk to extract
ifconfig eth0|grep "inet addr" |awk-f ' [:]+ ' {print $4} '
Ifconfig eth0|sed-n '/inet addr/p ' |awk-f ' [:]+ ' {print $4} '
4, directly using SED regular expression to match
ifconfig eth0|sed-rn ' S#^.*DR: (. *) B.*$#\1#GP '
Ifconfig eth0|sed-n ' s#^.*dr:\ (. *\) B.*$#\1#GP '
5. Match with awk regular expressions directly
Ifconfig eth0|awk-f ' [:]+ ' nr==2 {print $4} '
ifconfig eth0|awk-f ' [:]+ '/bcast/{print $4} '
Ifconfig eth0|awk-f ' [:]+ '/inet addr:/ {Print $4} '
Ifconfig eth0|awk-f ' [:]+ ' $ $ ~ ' inet addr ' {print $4} '
6, directly with the grep regular expression to match the IP address number
Ifconfig eth0|grep-o ' \ ([1-9]\{1,3\}\.\) \{3\}[0-4]\{3,\} '
#####[0-4] do not exceed 4 otherwise the subnet mask will be extracted.
7.
IP Addr|grep-po ' [^]+ (? =/\d) ' |sed-n ' 3p '
8. Direct extraction of IP address files.
/etc/sysconfig/network-scripts/Ifcfg-eth0
Cat/etc/sysconfig/network-scripts/ifcfg-eth0|sed-n ' 10p ' |cut-d "="-f2
Under Linux, how to execute the ifconfig command with regular expressions, only extract the IP address!