- A+
处理以下文件内容,将域名取出并根据域名进行计数排序处理:(百度和sohu面试题)
1 2 3 4 5 6 | http://www.drscrewdriver.com/index.html http://www.drscrewdriver.com/1.html http://post.drscrewdriver.com/index.html http://mp3.drscrewdriver.com/index.html http://www.drscrewdriver.com/3.html http://post.drscrewdriver.com/2.html |
要求结果:
1 2 3 | mp3.drscrewdriver.com 1 post.drscrewdriver.com 2 www.drscrewdriver.com 3 |
思路:
- 取出域名
- 以斜线为菜刀取出第二列(域名)
- 进行加工
- 创建一个数组
- 把第二列(域名)作为数组的下标
- 通过类似于i++的形式进行计算数量
- 统计后把结果输出
答案:
1 | awk -F "/+" '{hotel[$2]++}END{for(pol in hotel) print pol,hotel[pol]}' url.txt|sort -rnk2 |
演示:
1 2 3 4 | [root@show awkfile]# awk -F "/+" '{hotel[$2]++}END{for(pol in hotel) print pol,hotel[pol]}' url.txt|sort -rnk2 www.drscrewdriver.com 3 post.drscrewdriver.com 2 mp3.drscrewdriver.com 1 |
我们先不要着急整体分析这个结果,我们把这个结果分为几个里程碑逐个击破。
第一个里程碑-取出想要的内容
根据题目要求我们需要取出域名。awk -F "/+" ,需要用到+表示连续的。
1 2 3 4 5 6 7 | [root@youjiu awkfile]# awk -F "/+"'{print $2}' url.txt www.drscrewdriver.com www.drscrewdriver.com post.drscrewdriver.com mp3.drscrewdriver.com www.drscrewdriver.com post.drscrewdriver.com |
第二个里程碑-创建数组
我们接下来创建数组,数组名字还是hotel,每个元素(房间)
1 2 3 4 5 6 7 8 | [root@show awkfile]# awk -F "/+"'{hotel[$2]}' url.txt ###创建数组 [root@show awkfile]# awk -F "/+"'{hotel[$2];print $2}' url.txt ##创建数组,并通过print 输出元素名字(房间号码) www.drscrewdriver.com www.drscrewdriver.com post.drscrewdriver.com mp3.drscrewdriver.com www.drscrewdriver.com post.drscrewdriver.com |
第三个里程碑-进行统计
1 2 3 4 5 6 7 8 | [root@youjiu awkfile]# awk -F "/+"'{hotel[$2]++}' url.txt ###创建数组 [root@youjiu awkfile]# awk -F "/+"'{hotel[$2]++;print $2,hotel[$2]}' url.txt ##创建数组,并通过print 输出元素名字(房间号码),房间内容 www.drscrewdriver.com 1 www.drscrewdriver.com 2 post.drscrewdriver.com 1 mp3.drscrewdriver.com 1 www.drscrewdriver.com 3 post.drscrewdriver.com 2 |
$2表示的是每一行的第二列,是一个变量。
hotel[$2]++这种形式类似于前面的i++,只不过把变量i换成了数组hotel[$2],相当于把原来的一个插间的房子,换成了一个公寓楼。
下面我们详细分析下awk如何统计www.drscrewdriver.com重复了多少次。
这里我们只关注www.drscrewdriver.com重复的次数。
读取第一行:
以"/+"连续的/为菜刀,切割$2就是www.drscrewdriver.com,
把他放在数组中就是hotel["www.drscrewdriver.com"],
进行统计 hotel["www.drscrewdriver.com"]=hotel["www.drscrewdriver.com"]+1
hotel酒店中www.drscrewdriver.com房间原来没有东西,可以理解为空的。所以hotel["www.drscrewdriver.com"]=空+1 最后这个房间放入了数字1.
读取第二行:
$2还是www.drscrewdriver.com.
统计就是 hotel["www.drscrewdriver.com"]=hotel["www.drscrewdriver.com"]+1
因为上面我们已经往hotel酒店的www.drscrewdriver.com房间放入数字1,所以现在
hotel["www.drscrewdriver.com"]=1+1hotel酒店www.drscrewdriver.com房间内容应该是2
读取第三行:
$2是post.drscrewdriver.com.
不是我们想要的www.drscrewdriver.com所以www.drscrewdriver.com房间内容还是2不会变化。
读取第四行:
$2是mp3.drscrewdriver.com
不是我们想要的www.drscrewdriver.com所以www.drscrewdriver.com房间内容还是2不会变化。
读取第五行:
$2还是www.drscrewdriver.com.
统计就是 hotel["www.drscrewdriver.com"]=hotel["www.drscrewdriver.com"]+1
因为上面我们已经往hotel酒店的www.drscrewdriver.com房间放入数字2,所以现在
hotel["www.drscrewdriver.com"]=2+1hotel酒店www.drscrewdriver.com房间内容应该是3
读取第六行:
$2是post.drscrewdriver.com
不是我们想要的www.drscrewdriver.com所以www.drscrewdriver.com房间内容还是3不会变化。
详细过程表格
只查看hotel["www"]房间的内容
行号 | $2的内容 | hotel["www"]之前的内容 | hotel["www"] = hotel["www"] + 1的过程 | hotel["www"]之后的内容 |
1 | www | 空 | hotel["www"] = 空 + 1 | 1 |
2 | www | 1 | hotel["www"] = 1 + 1 | 2 |
3 | post | 2 | 不是www不进行加1 | 2 |
4 | mp3 | 2 | 不是www不进行加1 | 2 |
5 | www | 2 | hotel["www"] = 2 + 1 | 3 |
6 | post | 3 | 不是www不进行加1 | 3 |
总结:
最终结果是:
1 2 3 4 | [root@show awkfile]# awk -F "/+" '{hotel[$2]++}END{for(pol in hotel) print pol,hotel[pol]}' url.txt|sort -rnk2 www.drscrewdriver.com 3 post.drscrewdriver.com 2 mp3.drscrewdriver.com 1 |
优化后的结果:
1 2 3 4 | [root@show awkfile]# awk -F "/+" '{hotel[$2]++}END{for(pol in hotel) print pol,hotel[pol]}' url.txt|sort -rnk2|column -t www.drscrewdriver.com 3 post.drscrewdriver.com 2 mp3.drscrewdriver.com 1 |
可以通过,column 命令让结果优雅一些,比awk的printf方便些。
今天是老男孩教育每日一题陪伴大家的第19天。
对于题目和答案的任何疑问,请在博客评论区留言。
往期题目索引
