shell中,统计一个ip在日志文件中出现的次数

  |   0 评论   |   0 浏览

shell脚本如下

#!/bin/bash
hosts=(172.17.0.24 172.17.0.29 172.17.0.21 172.17.0.32 172.17.0.13 172.17.0.20 172.17.0.19 172.17.0.7 172.17.0.9 172.17.0.15 172.17.0.25 172.17.0.47 172.17.0.16 172.17.0.46 172.17.0.49 172.17.0.6)

echo "共有servers: ${#hosts[*]} 台"

for host in ${hosts[*]}
do
count=`grep $host $1 | wc -l`
printf "$host\t$count\n"
done

用法:

[root@VM_112_36_centos ~]# sh tj.sh preorder.log 
172.17.0.24	0
172.17.0.29	27388
172.17.0.21	0
172.17.0.32	0
172.17.0.13	27109
172.17.0.20	27320
172.17.0.19	27243
172.17.0.7	27407
172.17.0.9	27222
172.17.0.15	27338
172.17.0.25	27375
172.17.0.47	27192
172.17.0.16	27486
172.17.0.46	27057
172.17.0.49	27210
172.17.0.6	27205

标题:shell中,统计一个ip在日志文件中出现的次数
作者:SmiteLi
地址:https://smite.site/articles/2020/02/12/1581511669980.html