awk Commands Simple Learning

Source: Internet
Author: User

Please execute the command to take out the IP address of eth0 in Linux (please use cut, the ability can also be used separately awk,sed command answer).

Answer:

Description: This solution has been explained to you no less than 15 kinds, but also can have a lot of, here to everyone to focus on the use of awk, I hope that we can pay more attention to the teacher's thinking, thinking process, but the answer to focus on the problem, in order to achieve the effect of learning a pass 10.

Method One "This method for green leaves": If you use awk to deal with, the general students may use the following methods to achieve:

[[email protected] ~]# ifconfig eth0|grep ' inet addr ' |awk-f ': ' {print $} ' |awk ' {print $} '

10.0.0.162

Summary: This method is a regular method, very good. But do we have a simpler method of awk implementation? Look down.

Method Two "This method is small safflower": using awk to use multiple separators at the same time to achieve:

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

192.168.1.186

Tip: The subject by using a space or a colon to do the delimiter, and then take out the result, that is, a space or a colon can be regarded as the same delimiter.

Summary: This method is very convenient, students can use more, select the Special keyword ' inet addr ' filter that contains the IP address line and then use awk with spaces and: Do the delimiter at the same time, and then take out the required content.

The biggest drawback of this answer is to calculate the $n time is very laborious, because inet front there are several spaces, find $13, it is not easy, so it seems that the implementation of time may not be as fast as the first method? Well, that leads us to the key answer case for today.

Method three "Big Red Flower Appearances": Using awk to use multiple separators at the same time, and then through the regular matching of multiple separators to achieve the method:

[[email protected] ~]# ifconfig eth0 |awk-f ' [:]+ ' nr==2 {print $4} '

10.0.0.185

Let's illustrate this easy-to-use good way:

Hint: NR is the line number, delimiter + number match, [] one or more of the delimiters, here is a match one or more colons or spaces.

1) awk-f followed by the delimiter ' [Space:]+ ', where [space:] Multi-separator notation, meaning is separated by a space or colon, followed by the "+" sign is a regular expression, meaning to match the preceding space or colon, either of the 1 or more than 1.

2) nr==2 and sed-n "2p", quite, means to select the first few lines, for example:

[Email protected] ~]# ifconfig Eth0|awk nr==2

inet addr:10.0.0.185 bcast:10.0.0.255 mask:255.255.255.0

3) After specifying the Awk-f ' [:]+ ' delimiter, the columns separated by different strings are:

inet addr 10.0.0.185 bcast:10.0.0.255 mask:255.255.255.0

The second column in the first column, column fourth, is ignored after the third row.

4) The entire answer to awk part means that the second line is fetched through nr==2, then the match is done through-f ' [-:]+ multi-delimited regular, then the fourth column is printed through {print $4}! Thank you Yangrui classmate of the answer ah, too lazy to write, so let you summarize under. Hey, don't blame the teacher when you see it.

5) Master The theory to practice verification is the scientific rigorous learning attitude, good, to a few examples of practice to speak it.

[[email protected] ~]# echo---1:----2|awk-f ' [-:]+ ' {print $} '

1

[[email protected] ~]# echo---1:----2|awk-f ' [-:]+ ' {print $} '

2

[[email protected] ~]# echo---1:::::2|awk-f ' [-:]+ ' {print $} '

2

awk Commands Simple Learning

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.