zoukankan      html  css  js  c++  java
  • 每日一题20180325

    一、题目

    1.1 统计

    处理以下文件内容,将域名取出并根据域名进行计数排序处理:(百度和sohu面试题)

    http://www.etiantian.org/index.html
    http://www.etiantian.org/1.html
    http://post.etiantian.org/index.html
    http://mp3.etiantian.org/index.html
    http://www.etiantian.org/3.html
    http://post.etiantian.org/2.html
    

    要求结果

    mp3.etiantian.org 1
    post.etiantian.org 2
    www.etiantian.org 3
    

    1.2

    统计企业工作中高并发web服务器不同网络连接状态对应的数量

    Proto Recv-Q Send-Q Local-Address          Foreign-Address             State
    tcp        0      0 0.0.0.0:3306           0.0.0.0:*                   LISTEN
    tcp        0      0 0.0.0.0:80             0.0.0.0:*                   LISTEN
    tcp        0      0 127.0.0.1:9000         0.0.0.0:*                   LISTEN
    tcp        0      0 coolshell.cn:80        124.205.5.146:18245         TIME_WAIT
    tcp        0      0 coolshell.cn:80        61.140.101.185:37538        FIN_WAIT2
    tcp        0      0 coolshell.cn:80        110.194.134.189:1032        ESTABLISHED
    tcp        0      0 coolshell.cn:80        123.169.124.111:49809       ESTABLISHED
    tcp        0      0 coolshell.cn:80        116.234.127.77:11502        FIN_WAIT2
    tcp        0      0 coolshell.cn:80        123.169.124.111:49829       ESTABLISHED
    tcp        0      0 coolshell.cn:80        183.60.215.36:36970         TIME_WAIT
    tcp        0   4166 coolshell.cn:80        61.148.242.38:30901         ESTABLISHED
    tcp        0      1 coolshell.cn:80        124.152.181.209:26825       FIN_WAIT1
    tcp        0      0 coolshell.cn:80        110.194.134.189:4796        ESTABLISHED
    tcp        0      0 coolshell.cn:80        183.60.212.163:51082        TIME_WAIT
    tcp        0      1 coolshell.cn:80        208.115.113.92:50601        LAST_ACK
    tcp        0      0 coolshell.cn:80        123.169.124.111:49840       ESTABLISHED
    tcp        0      0 coolshell.cn:80        117.136.20.85:50025         FIN_WAIT2
    tcp        0      0 :::22                  :::*                        LISTEN
    

    1.3

    分析图片服务日志,把日志(每个图片访问次数*图片大小的总和)排行,取top10,也就是计算每个url的总访问大小

    59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
    
    59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
    
    59.33.26.105 - - [08/Dec/2010:15:44:02 +0800] "GET /static/flex/vedioLoading.swf HTTP/1.1" 200 3583 "http://oldboy.blog.51cto.com/static/flex/AdobeVideoPlayer.swf?width=590&height=328&url=/`DYNAMIC`/2" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
    
    124.115.4.18 - - [08/Dec/2010:15:44:15 +0800] "GET /?= HTTP/1.1" 200 46232 "-" "-"
    
    124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/web_js.js HTTP/1.1" 200 4460 "-" "-"
    
    124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/jquery.lazyload.js HTTP/1.1" 200 1627 "-" "-"
    

    1.4

    假如现在有个文本,格式如下:

    a  1
    b  3
    c  2
    d  7
    b  5
    a  3
    g  2
    f  6
    d  9
    

    即左边是随机字母,右边是随机数字,要求写个脚本使其输出格式为:

    a  4
    b  8
    c  2
    d  16
    f  6
    g  2
    

    即将相同的字母后面的数字加在一起,按字母的顺序输出。

    1.5

    1、按单词出现频率降序排序!

    2、按字母出现频率降序排序!

    the squid project provides a number of resources to assist users design,implement and support squid installations. Please browse the documentation and support sections for more infomation
    

    二、答案

    2.1

    awk -F "/+" '{host[$2]++}END{for (i in host) print i,host[i]}' a.txt | sort -rnk2
    

    分析:

    # 01找出域名
    awk -F参数指定分割符 "/+" 表示以/或连续多个//为分割符
    
    # 02域名计数
    awk 命令结构BEGIN{}{}END{}
    定义数组host[$2]以域名为索引,数组的值为域名的统计数据
    循环结束后输出域名和域名统计数据
    
    # 03排序
    sort 
    -r参数表示倒序
    -n参数表示按数字排序
    -k2表示按第二列排序
    

    2.2

     awk '{print $NF}' a.txt | uniq -c
    

    分析:

    NF表示遍历的当前行的字段个数$NF就表示最后一个字段的值,最后一个字段的值就是网络状态
    uniq -c去重并计数
    

    2.3

    awk 'NF>0{url[$7]+=$10}END{for(i in url) print i, url[i]}' a.txt | sort -rnk2 | head -10
    

    分析:

    awk默认分割符是空格,第7列是访问的url,第10列是访问资源的大小
    由于日志有空行,NF>0表示该行没有项,即空行
    head -10 表示取前10行
    

    2.4

    awk '{alpha[$1]+=$2}END{for (i in alpha) print i, alpha[i]}' a.txt
    

    分析:和1.1思路类似

    2.5

    # 升序排
    cat a.txt|xargs -n1|awk '{word[$1]++}END{for(i in word)print i,word[i]}'|sort -nk2
    # 降序
    cat a.txt|xargs -n1|awk '{word[$1]++}END{for(i in word)print i,word[i]}'|sort -nrk2
    

    分析:

    一行转多行
    xargs -n1表示每行一个每行3个就-n3
    -dX表示把X作为分隔符,默认是空格
    

    三、参考

    老男孩教育每日一题-2017年3月31日-awk数组统计

    轻松精通awk数组企业问题案例

  • 相关阅读:
    centos 安装 谷歌BBR
    centos上mailx通过465端口发送邮件
    centos 6的LAMP一键安装包(可选择/升级版本)
    Linux 一键安装最新内核并开启 BBR 脚本
    CentOS 7安装Xfce和VNC
    重构手法之简化函数调用【4】
    重构手法之简化函数调用【3】
    重构手法之简化函数调用【2】
    重构手法之简化函数调用【1】
    重构手法之简化条件表达式【4】
  • 原文地址:https://www.cnblogs.com/okokabcd/p/8647614.html
Copyright © 2011-2022 走看看