• Linux三剑客awk数组、判断、循环


    awk数组

    数组应用场景

    数组的下标可以是任意形式,理解为高级语言的map表即可

    统计次数:统计每个用户ip出现的次数,统计每种状态码出现次数,统计系统中每个用户被攻击的次数,统计攻击者ip出现的次数

    求和:统计每个ip消耗的流量

    shell数组awk数组
    形式array[0]=oldboy array[1]=lidaoarray[0]=oldboy array[1]=lidao
    使用echo ${array[0]} ${array[1]}print array[0] array[1]
    批量输出数组的内容for i in ${array[*]}
    do
    echo $i
    done
    for(i in array)
    print a[i]

    awk数组中取变量不用$,awk中的$就用来取列,$0取一行

    • awk中的字母会被识别为变量,如果想使用字符串,需要用到双引号
    awk 'BEGIN{a[0]=12306; a[1]="lidao"; print a[0],a[1]}'
    
    • 1

    在这里插入图片描述

    • awk批量输出数组内容
    awk 'BEGIN{a[0]=12306; a[1]="lidao"; a[2]="hello"; a[3]=123; for(i in a) print i,a[i]};'
    
    • 1

    在这里插入图片描述

    • 统计下面每个URL出现的次数
    http://www.etiantain.org/index.html
    http://www.etiantain.org/1.html
    http://post.etiantain.org/index.html
    http://mp3.etiantain.org/index.html
    http://www.etiantain.org/3.html
    http://post.etiantain.org/2.html
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    awk -vFS=/ '{array[$3]++;}END{for (url in array) print url, array[url]}' url.txt
    awk -vFS=/ '{array[$3]++;}END{for (url in array) print url, array[url]}' url.txt | sort -rnk2
    
    • 1
    • 2

    在这里插入图片描述

    awk '$9~[0-9][0-9][0-9]/{array[$9]++}END{for (i in array) print i, array[i]}' access.log | sort -rnk2
    
    • 1

    在这里插入图片描述

    awk循环

    shell循环awk循环
    使用for(i=1;i<=10;i++)
    do
    echo $i
    done
    for(i=1;i<=10;i++) {print i}
    • 1-100求和
    awk 'BEGIN{for(i=1;i<=100;i++){sum+=i;} print sum;}'
    
    • 1

    awk判断

    在这里插入图片描述

    • 磁盘利用率大于20%,则显示"disk not enough"
    df -h | awk -F"[ %]+" 'NR>1{if($5>20){print "disk not enough", $1, $5,$NF}}'
    
    • 1

    在这里插入图片描述

    awk使用条件判断时,第一个条件在{动作}前,后面还有条件判断用if

    • 统计一段文本中,单词长度小于6的单词数量
    Studying a subject that you feel pointless is never a fun or easy task. If you're study history, asking yourself the question "why is history important" is a very good first step. History is an essential part of human civilization. You will find something here that will arouse your interest, or get you thinking about the significance of history.
    
    • 1
    shen@ubuntu-vm:~/code/test$ awk -F"[ .\",]+" '{for(i=1;i<=NF;i++){print $i}}' en.txt 
    Studying
    a
    subject
    that
    you
    feel
    pointless
    is
    never
    a
    fun
    or
    easy
    task
    If
    you're
    study
    history
    asking
    yourself
    the
    question
    why
    is
    history
    important
    is
    a
    very
    good
    first
    step
    History
    is
    an
    essential
    part
    of
    human
    civilization
    You
    will
    find
    something
    here
    that
    will
    arouse
    your
    interest
    or
    get
    you
    thinking
    about
    the
    significance
    of
    history
    
    shen@ubuntu-vm:~/code/test$ awk -F"[ .\",]+" '{for(i=1;i<=NF;i++){if(length($i)>6)print $i}}' en.txt 
    Studying
    subject
    pointless
    history
    yourself
    question
    history
    important
    History
    essential
    civilization
    something
    interest
    thinking
    significance
    history
    shen@ubuntu-vm:~/code/test$ awk -F"[ .\",]+" '{for(i=1;i<=NF;i++){if(length($i)>6) sum+=1}} END{print sum}' en.txt 
    16
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
  • 相关阅读:
    12月份PMP考试首次采用新考纲,该怎么学?
    web大学生网页作业成品 响应式网站水果超市7页(html+css+javascript+jquery+bootstarp)
    什么? CSS 将支持 if() 函数了?
    【计算机网络笔记】DNS报文格式
    Mac电脑idea中配置nodejs前端环境
    验证的挑战
    LeetCode 1779. 找到最近的有相同 X 或 Y 坐标的点
    实验三 图像分割实验
    英特尔近30年来首次季度亏损,市值被AMD反超
    服务访问质量
  • 原文地址:https://blog.csdn.net/qq_42500831/article/details/126368469