Under Linux, how to execute the ifconfig command with regular expressions, only extract the IP address!

Source: Internet
Author: User
Tags grep regular expression

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.