列表

详情


SHELL23. nginx日志分析1-IP统计

描述

假设nginx的日志我们存储在nowcoder.txt里,格式如下:
192.168.1.20 - - [21/Apr/2020:14:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [21/Apr/2020:15:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [21/Apr/2020:21:27:49 +0800] "GET /3/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.23 - - [21/Apr/2020:22:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.24 - - [22/Apr/2020:15:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.25 - - [22/Apr/2020:15:26:49 +0800] "GET /3/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.20 - - [23/Apr/2020:08:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [23/Apr/2020:09:20:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:10:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:10:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.20 - - [23/Apr/2020:14:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [23/Apr/2020:15:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:15:27:49 +0800] "GET /3/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.25 - - [23/Apr/2020:16:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.24 - - [23/Apr/2020:20:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.25 - - [23/Apr/2020:20:27:49 +0800] "GET /3/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.20 - - [23/Apr/2020:20:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [23/Apr/2020:20:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:20:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:15:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [23/Apr/2020:20:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
现在需要你统计出2020年4月23号的访问ip次数,并且按照次数降序排序。你的脚本应该输出:
5 192.168.1.22
4 192.168.1.21
3 192.168.1.20
2 192.168.1.25
1 192.168.1.24

原站题解

Bash 解法, 执行用时: 3ms, 内存消耗: 428KB, 提交时间: 2021-12-04

declare -A map
while read line
    do
        tmp=($line)
        [[ ${tmp[3]} =~ 23/Apr ]] && ((map["${tmp[0]}"]++))
    done < nowcoder.txt
function ShellSort(){    #希尔排序
    tmp=()
    for ve in ${map[*]}
        do
            tmp[${#tmp[*]}]=$ve
        done
    q=${#tmp[*]}
for (( gap=q>>1; gap>0; gap=gap>>1 ))
    do
        for (( i=gap; i<q; i++ ))
            do
                temp=${tmp[$i]}
                for (( a=i-gap; a>=0&temp>${tmp[$a]}; a-=gap ))
                    do
                        tmp[$a+$gap]=${tmp[$a]}
                    done
                tmp[$a+$gap]=$temp
            done
    done
}
ShellSort
for ((i=0; i<$q; i++))
    do
        for ve in ${!map[*]}
            do
                if [ ${tmp[$i]} -eq ${map[$ve]} ];then
                    printf "${map[$ve]} $ve\n"
                fi
            done
    done

Bash 解法, 执行用时: 4ms, 内存消耗: 420KB, 提交时间: 2022-04-13

#!/bin/bash
#!/bin/awk
declare -A map
while read line
    do
        tmp=($line)
        [[ ${tmp[3]} =~ 23/Apr ]] && ((map["${tmp[0]}"]++))
    done < nowcoder.txt
function ShellSort(){
    tmp=()
    for ve in ${map[*]}
        do
            tmp[${#tmp[*]}]=$ve
        done
    q=${#tmp[*]}
for (( gap=q>>1; gap>0; gap=gap>>1 ))
    do
        for (( i=gap; i<q; i++ ))
            do
                temp=${tmp[$i]}
                for (( a=i-gap; a>=0&temp>${tmp[$a]}; a-=gap ))
                    do
                        tmp[$a+$gap]=${tmp[$a]}
                    done
                tmp[$a+$gap]=$temp
            done
    done
}
ShellSort
for ((i=0; i<$q; i++))
    do
        for ve in ${!map[*]}
            do
                if [ ${tmp[$i]} -eq ${map[$ve]} ];then
                    printf "${map[$ve]} $ve\n"
                fi
            done
    done

Bash 解法, 执行用时: 4ms, 内存消耗: 512KB, 提交时间: 2021-12-20

declare -A map
while read line
    do
        tmp=($line)
        [[ ${tmp[3]} =~ 23/Apr ]] && ((map["${tmp[0]}"]++))
    done < nowcoder.txt
function ShellSort(){
    tmp=()
    for ve in ${map[*]}	#map的值转到tmp数组里
        do
            tmp[${#tmp[*]}]=$ve
        done
    q=${#tmp[*]}
	for (( gap=q>>1; gap>0; gap=gap>>1 )) #希尔排序,对tmp数组进行排序
	    do
	        for (( i=gap; i<q; i++ ))
	            do
	                temp=${tmp[$i]}
	                for (( a=i-gap; a>=0&temp>${tmp[$a]}; a-=gap ))
	                    do
	                        tmp[$a+$gap]=${tmp[$a]}
	                    done
	                tmp[$a+$gap]=$temp
	            done
	    done
}
ShellSort
for ((i=0; i<$q; i++))
    do
        for ve in ${!map[*]}
            do
                if [ ${tmp[$i]} -eq ${map[$ve]} ];then
                    printf "${map[$ve]} $ve\n"
                fi
            done
    done

Bash 解法, 执行用时: 4ms, 内存消耗: 552KB, 提交时间: 2022-01-21

declare -A map
while read line
    do
        tmp=($line)
        [[ ${tmp[3]} =~ 23/Apr ]] && ((map["${tmp[0]}"]++))
    done < nowcoder.txt
function ShellSort(){
    tmp=()
    for ve in ${map[*]}  #map的值转到tmp数组里
        do
            tmp[${#tmp[*]}]=$ve
        done
    q=${#tmp[*]}
    for (( gap=q>>1; gap>0; gap=gap>>1 )) #希尔排序,对tmp数组进行排序
        do
            for (( i=gap; i<q; i++ ))
                do
                    temp=${tmp[$i]}
                    for (( a=i-gap; a>=0&temp>${tmp[$a]}; a-=gap ))
                        do
                            tmp[$a+$gap]=${tmp[$a]}
                        done
                    tmp[$a+$gap]=$temp
                done
        done
}
ShellSort
for ((i=0; i<$q; i++))
    do
        for ve in ${!map[*]}
            do
                if [ ${tmp[$i]} -eq ${map[$ve]} ];then
                    printf "${map[$ve]} $ve\n"
                fi
            done
    done

Bash 解法, 执行用时: 5ms, 内存消耗: 1248KB, 提交时间: 2021-11-27

awk '{
    if(substr($4, 2, 11) == "23/Apr/2020") res[$1]++;
}END{
    for(k in res){
        print res[k] " " k
    }
}' | sort -nr -k1

上一题