process The following file contents , take out the domain name and sort the order according to the domain name :( Baidu and Sohu face test )
For a detailed explanation of the answers, please refer to:
http://lidao.blog.51cto.com/3388056/1912219
File contents:
http://www.etiantian.org/index.htmlhttp://www.etiantian.org/1.htmlhttp://post.etiantian.org/index.htmlhttp:// Mp3.etiantian.org/index.htmlhttp://www.etiantian.org/3.html
Required results:
mp3.etiantian.org 1post.etiantian.org 2www.etiantian.org 3
Answer:
| 1 |
awk-F "/+" ‘{hotel[$2]++}END{for(pol in hotel) print pol,hotel[pol]}‘url.txt|sort -rnk2 |
Let's take a look at the problem with this question.
First of all, what are our goals and what are our results?
Here we simplify the problem, the goal is simplified to statistical Www,mp3.post repeated several times, the reader can adjust themselves, and statistics www.etiantian.org,mp3.etiantian.org, post.etiantian.org these domain names.
www has appeared several times
MP3 appeared several times.
The post appeared several times
if a www is present, the number of repetitions of www is recorded with a , and if mp3 appears then B is used to record the number of repetitions of MP3 , If post is present, the repeat quantity of post is recorded by C .
The answers are as follows:
[[email protected]] #awk-F "[/]+" ' $2~/www/{a++}$2~/mp3/{b++}$2~/post/{c++}end{print "www:" A, "MP3:" B, "Post:" C} ' Url.txt
Demo Process:
[Email protected]]# awk-f "[/]+" ' $2~/www/{a++}$2~/mp3/{b++}$2~/post/{c++}end{print "www:" A, "MP3:" B, "Post:" C} ' Url.txtwww:3 mp3:1 Post:2
Although this is easy to understand, because there are only three kinds of domain name Www,post,mp3 , the general work will have a lot of domain names, you will be crazy.
So we want a method, a thing (a name) can be loaded in these three cases, and do not affect each other.
This is what we call an array (hotel), and these three cases are the elements of the array we're talking about (hotel rooms).
Or just the idea, divided into three kinds of situations, the different circumstances, this time is put into the hotel's three rooms.
[[email protected] awkfile] #awk-F "[/]+" ' $2~/www/{h["www"]++}$2~/mp3/{h["MP3"]++}$2~/post/{h["POST"]++}end{printh ["www"],h["MP3"],h["POST"]} ' URL.TXT3 1 2
Tip: If you specify a room name (the array element name) for the awk array, be sure to add double quotes. Otherwise , awk will think of this as a variable.
[[email protected] awkfile] #awk ' Begin{h[www]=1;h[bbs]=2;print H[www],h[bbs]} ' 2 2[[email protected] awkfile] #awk ' begin{h["www"]=1;h["bbs"]=2;printh["www"],h["BBS"]} ' 1 2
The example above is that if you don't add double quotes,awk will consider it a variable.
Still a lot of trouble how to solve it?
Is there any way to get into the same room when you encounter the same content?
If you encounter www automatically enter www room
If you encounter MP3 automatically enters MP3 room
If you encounter post automatically enter the post room
yes, think about how we get www or mp3 or post through awk ?
They are the second column! awk can be used as a notation.
so , this array can be written as.
h[$2]++ , auto-seat, after the self-added operation (Auto plus 1).
[[email protected] awkfile] #awk-F "[/.] + "' {h[$2]++;p rint h[" www "]} ' Url.txt122233[[email protected]show Awkfile] #awk-F" [/.] + "' {h[$2]++;p rint" www: "h[" www "]," MP3: "h[" MP3 "]," Post: "h[" POST "]} ' url.txtwww:1 mp3:post:www:2 mp3:post:www:2 mp3: Post:1www:2 mp3:1 post:1www:3 mp3:1 post:1www:3 mp3:1 post:2 So although we can see the results we want, but also show the implementation process, we just want results!
as long as the final result can be passed END mode to resolve.
[[email protected] awkfile] #awk-F "[/.] + "' {h[$2]++}end{print" www: "h[" www "]," MP3: "h[" MP3 "]," Post: "h[" POST "]} ' Url.txtwww:3 mp3:1 post:2
This is a room to show the contents of a room, or that sentence when the room a lot of time, you are crazy.
awk naturally has a way of solving this problem, dedicated loops.
[[email protected] awkfile] #awk-F "[/.] + "' {h[$2]++}end{for (Pol in h) print Pol,h[pol]} ' url.txtwww 3mp3 1post 2
He can do more complicated calculations, and of course we can do the most commonly used statistics (de-weight).
It would be a nightmare if we had to use the room number manually to specify the room.
The Www,mp3,post can be a good indication of the content. Of course, it involves the prior knowledge, choose the right knife, cut out what you want.
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/92/3F/wKiom1j9piqyAjPYAAKAbD6GgNs208.png "title=" Old boy It education-awk array calculation process-Take www as an example. png "alt=" Wkiom1j9piqyajpyaakabd6ggns208.png "/>
awk Array Statistics count (de-weight)
http://lidao.blog.51cto.com/3388056/1912219
awk Array to accumulate (calculate sum)
http://lidao.blog.51cto.com/3388056/1914563
This article is from the "Lee blog" blog, make sure to keep this source http://lidao.blog.51cto.com/3388056/1918859
Beginner awk Arrays will encounter several common problems