Execute command to take out the IP address of eth0 in Linux

Source: Internet
Author: User

Method One: Cut command

#ifconfig eth0|grep ' inet addr ' |cut-d ":-f2|cut-d" "-f1192.168.1.11

Method Two: awk command

1. Use the awk command two times

#ifconfig eth0 |grep ' inet addr ' |awk-f ': ' {print $} ' |awk ' {print $} ' 192.168.1.11

2. Use the awk command again

Error wording:

# ifconfig eth0|grep ' inet addr ' |awk-f [:] ' {print $13} ' awk::]awk: ^ syntax error

Correct wording:

#ifconfig eth0|grep ' inet addr ' |awk-f ' [:] ' {print $13} ' 192.168.1.11

It can also be:

#ifconfig eth0|sed-n ' 2p ' |awk-f ' [:] ' {print $ (NF-6)} '

NF represents the last line in awk, if you do not understand the minus 6 after NF, you can view it separately

[[email protected] lele]# ifconfig eth0|sed -n  ' 2p '            inet addr:192.168.1.11  bcast:192.168.1.255  mask :255.255.255.0[[email protected] lele]# ifconfig eth0|sed -n  ' 2p ' |awk -F   ' [ :] '   ' {print $ (NF)} ' 255.255.255.0[[email protected] lele]# ifconfig  eth0|sed -n  ' 2p ' |awk -f  ' [ :] '   ' {print $ (NF-1)} ' mask[[email  protected] lele]# ifconfig eth0|sed -n  ' 2p ' |awk -f  ' [ :] '   ' {print  $ (NF-2)} ' [[email protected] lele]# ifconfig eth0|sed -n  ' 2p ' |awk -F   ' [ :] '   ' {print $ (NF-3)} ' 192.168.1.255[[email protected] lele]# ifconfig  eth0|sed -n  ' 2p ' |awk -f  ' [ :] '   ' {print $ (NF-4)} ' bcast[[email  Protected] lele]# ifconfig eth0|sed -n  ' 2p ' |awk -f  ' [ :] '   ' {print $ (NF-5)} ' [[email  protected] lele]# ifconfig eth0|sed -n  ' 2p ' |awk -f  ' [ :] '   ' {print  $ (NF-6)} ' 192.168.1.11

3.awk multiple Separators

#ifconfig eth0|sed-n ' 2p ' |awk-f ' [:]+ ' {print $4} ' 192.168.1.11#ifconfig eth0|awk-f ' [:]+ ' nr==2 {print $4} ' 192.168. 1.11

' + ' means repeating the preceding multiple characters, combining multiple spaces or colons into one character.



Execute command to take out the IP address of eth0 in Linux

Related Article

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.