AWK-F option to specify multiple symbols as delimiters when encountering a space pit, title: Please execute the command to remove the eth0 IP address from Linux

Source: Internet
Author: User

The third pre-course exam question: 3. Please execute the command to take out the IP address of eth0 in Linux (please use cut, the ability can also be used awk,sed command answer respectively)


Because there was a question about awk-f. You can specify an input delimiter, or you can use multiple symbols to cut a file at the same time, so I decided to use Awk-f first.

Examples of previous awk-f specifying multiple delimiters:

[email protected] ~]# cat Oldboy.txti am OLDBOY,MYQQ is 31333741[[email protected] ~]# awk-f "[,]" ' {print $3,$6} ' oldb Oy.txt Oldboy 31333741


Then start the question: Please execute the command to remove the eth0 IP address from Linux

[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $} ' [[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $} ' [[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $} ' [[Email Prote CTED] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $4} '

Printing to the fourth column, nothing is output to the screen, and then my head starts to pop up with a bunch of question marks ... What's the situation? Don't play games with me?

All right, try printing all the columns with a.

[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $} ' inet addr:192.168.56.130 bcast:192 .168.56.255 mask:255.255.255.0

No problem!

Strange!

I looked closely at the text and found that there were two spaces between inet addr:192.168.56.130 and bcast:192.168.56.255 and mask:255.255.255.0, I guess it might be because of this (and finally proving that this is not the root cause), so it might not be possible to use a space as a separator? At this time think of a + sign can use a number of consecutive same symbol as a separator, try!

[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ': |[]+ ' {print $4} ' 192.168.56.130

Ha! It worked!

But the previous command [[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $4} ' did not have any error hints, the description should be correct, but why is there no output?

Print out the last column and the Countdown column and try it.

[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $NF} ' 255.255.255.0[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $ (NF-1)} ' mask[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' { Print $ (NF-2)} ' [[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $ (NF-3)} ' 192.168.56.255

No problem, it means the order is correct.

Finally I printed the whole column and looked at it carefully:

[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' {print $} ' inet addr:192.168.56.130 bcast:192 .168.56.255 mask:255.255.255.0

Hey? The front of this line of characters seems to have some spaces! Is every space divided into a column???

wtf! I was a bunch of small insignificant space to the pit! Try again.

[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |awk-f ' [:] ' ' {print $13} ' 192.168.56.130

Finally showed up ...


At this point, the problem is finally clear, the original method is not wrong, the problem is that this line of characters at the beginning of the 10 spaces, will occupy 10 columns, so the first 10 columns to print, only the output of an empty content, and like I just get into the small white, it is easy to think of a mistake.


Other solutions to this problem, I will continue to add when there is time. The other solution of the general idea is almost the same, now can think of at least four or five kinds of solutions, but in fact all are the new, as long as the core thought, will be a solution, a little flexibility, and become another solution.

This article is from "Laopan linux" blog, please be sure to keep this source http://oldpan.blog.51cto.com/1603893/1969155

AWK-F option to specify multiple symbols as delimiters when encountering a space pit, title: Please execute the command to remove the eth0 IP address from Linux

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.