美文网首页
一道滴滴面试题

一道滴滴面试题

作者: LI木水 | 来源:发表于2018-09-30 13:25 被阅读0次

    有一张表table只有一列,统计出现次数大于2的行

    name
    b
    a
    c
    c
    c
    b
    d
    d
    d
    a
    c
    c
    c
    a

    1.请使用sql实现

    select name,count(1) as num from table group by name having num>2;
    

    解答:

    首先需要分组,对分组结果进行过滤需要使用having关键字
    

    2,假如这是一个文本文件test.txt,请使用Linux命令实现同样功能

    sort test.txt |uniq -c|sort -nr |awk '$1>2'
    

    解答:

    首先需要对文件排序使相同的行聚合在一起

    sort test.txt 
    
    a
    a
    a 
    b
    b 
    c
    c
    c
    c
    c
    c
    d
    d
    d
    

    使用uniq -c命令统计每行出现的次数

    sort test.txt |uniq -c
    
        3 a
        2 b
        6 c
        3 d
    
    

    使用awk过滤出现次数大于2的行

    sort test.txt |uniq -c|awk '$1>2'
    
    
          3 a
          6 c
          3 d
    
    

    对结果进行排序:-n排序,-r反转排序结果

    sort test.txt |uniq -c|awk '$1>2'|sort -nr
    
          6 c
          3 d
          3 a
    
    

    3.输出出现次数大于2小于5的行

    sort test.txt |uniq -c|sort -nr |awk '$1>2 && $1<=5'
    

    4.假如有100w行,输出出现次数最多的前100行

    sort test.txt |uniq -c|sort -nr |awk '$1>2' |head -100
    

    参考:

    http://man.linuxde.net/sort

    http://man.linuxde.net/uniq

    http://man.linuxde.net/awk

    http://man.linuxde.net/head

    相关文章

      网友评论

          本文标题:一道滴滴面试题

          本文链接:https://www.haomeiwen.com/subject/xykwoftx.html