我有一个包含多个数据的日志文件,我想从该文件中过滤DHCPREQUEST IP,我想计算日志文件中该服务内容的IP请求的次数是这样的: -
Mar 22 11:20:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2
Mar 22 11:19:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.2 from aa:00:00:00:00:00 (pc) via 10.1.1.2
Mar 22 11:18:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.2 from aa:00:00:00:17:00 (pc) via 10.1.1.2
Mar 22 11:16:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2
Mar 22 11:15:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2
Mar 22 11:14:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2
Mar 22 11:12:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2
Mar 22 11:11:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2
使用此代码我使用grep IP打印计数但是它显示重复IP的计数1也请建议我做错了
five_min='Mar 22 11:15:34'
while IFS= read -r line; do
if [[ "$five_min" < "$line" ]]
then
echo "$line" | grep DHCPREQUEST | awk -F " " '{print $8 }' | uniq -c
fi
done < file.txt
答案 0 :(得分:2)
您在单行上运行命令,因此您获得1是有意义的。
拥有以下 test.txt :
Mar 22 11:20:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2
Mar 22 11:20:35 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2
Mar 22 11:20:36 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2
命令:
grep DHCPREQUEST test | awk '{print $8}' | uniq -c
打印:
3 10.1.1.1
答案 1 :(得分:1)
关注单awk
也可以帮助您。
awk 'match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){array[substr($0,RSTART,RLENGTH)]++} END{for(i in array){print array[i],i}}' Input_file
现在也添加非单线形式的解决方案。
awk '
match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){ array[substr($0,RSTART,RLENGTH)]++ }
END{
for(i in array) { print array[i],i }
}
' Input_file