Learning awk from the command line to get the eth0 IP address in linux

Source: Internet
Author: User
From the command line to get the eth0 IP address in linux to learn awk, please execute the command to retrieve the eth0 IP address in linux (please use cut, the ability can also use awk, sed command to answer ). Answer: Note: There are no more than 15 methods to answer this question. here, I would like to emphasize on them... from the command line to get the eth0 IP address in linux to learn awk, please execute the command to retrieve the eth0 IP address in linux (please use cut, the ability can also use awk, sed command to answer ). Answer: Note: There are no more than 15 methods to answer this question. here we will focus on the usage of awk, I hope you can pay more attention to the teachers' ideas and thinking processes, but pay too much attention to the answers to the questions, so as to achieve a 10-minute learning effect. Method 1 [this method is green leaf]: if awk is used for processing, the average user may use the following method: [root @ oldboy ~] # Ifconfig eth0 | grep 'inet addr '| awk-F ":"' {print $2} '| awk' {print $1} '10. 0.0.162 Summary: This method is a common method and is good. But do we have a simpler awk implementation method? Look down. Method 2 [this method is a small safflower]: The awk uses multiple separators at the same time: [root @ oldboy ~] # Ifconfig eth0 | grep 'inet addr '| awk-F' [:] ''{print $13}' 192. 168.1.186 prompt: This question uses space or colon as the separator, and then gets the result at one time, that is, space or colon can be seen as the same separator. Summary: This method is very convenient. you can use it for many purposes. select the special keyword 'inet addr 'containing the IP address line to filter and use awk to separate spaces and, then extract the required content. The biggest disadvantage of this answer is that it is very difficult to calculate $ n, because there are multiple spaces in front of inet and it is not easy to find $13, in this case, the implementation time may not be as fast as the first method? Well, let's look at our key answer cases today. Method 3 [big safflower appearance]: use multiple separators at the same time through awk, and then use the regular expression to match multiple delimiters: [root @ oldboy ~] # Ifconfig eth0 | awk-F' [:] + ''nr = 2 {print $4} '10. 0.0.185: Let's explain this simple and easy-to-use method: Tip: This question NR is a line number, separator + number matching, one or more of the [] any separator, here, one or more colons or spaces are matched. 1) awk-F is followed by the separator '[space:] +', where [space:] Multiple separators are written, which means to be separated by spaces or colons, the plus sign ("+") is a regular expression that matches the preceding space or colon. one or more of the two are supported. 2) NR = 2 and sed-n "2 p", which means to select the row number, for example, [root @ oldboy ~] # Ifconfig eth0 | awk NR = 2 inet addr: 10.0.0.185 Bcast: 10.0.0.255 Mask: awk 0000000003) after specifying the awk-F' [:] + 'separator, the columns separated by different strings are: inetaddr10.0.0.185 Bcast: 10.0.0.255 Mask: 255.255.255.0. The first column, the second column, the third column, and the fourth column are ignored. 4) The awk part of the answer indicates that the second line is taken out through NR = 2, and then the regular match is performed through-F' [-:] + Multiple separators, print the fourth column through {print $4! Thank you, Yang Rui, for your answers. Hey, don't blame the teacher when you see it. 5) If you have mastered the theory, it is scientific and rigorous to go to practical verification. well, let's talk about it in a few examples. [Root @ oldboy ~] # Echo --- 1: ---- 2 | awk-F' [-:] + ''{print $2} '1 [root @ oldboy ~] # Echo --- 1: ---- 2 | awk-F' [-:] + ''{print $3} '2 [root @ oldboy ~] # Echo --- 1 :::: 2 | awk-F' [-:] + ''{print $3} '2
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.