Linux 取访问前十的ip地址 head -10 日志分析

[root@vm_dsj_20150527a logs]# cat test_err.log
/Get http://192.168.1.100/auth
/Get http://192.168.1.101/auth
/Get http://192.168.1.101/auth
/Get http://192.168.1.100/auth
/Get http://192.168.1.100/auth
/Get http://192.168.1.102/auth
/Get http://192.168.1.101/auth
/Get http://192.168.1.100/auth
/Get http://192.168.1.102/auth

截取ip地址: 方法一:

[root@vm_dsj_20150527a logs]# awk -F / {print $4} test_err.log
192.168.1.100
192.168.1.101
192.168.1.101
192.168.1.100
192.168.1.100
192.168.1.102
192.168.1.101
192.168.1.100
192.168.1.102

方法二:

[root@vm_dsj_20150527a logs]# cut -d / -f 4  test_err.log
192.168.1.100
192.168.1.101
192.168.1.101
192.168.1.100
192.168.1.100
192.168.1.102
192.168.1.101
192.168.1.100
192.168.1.102
[root@vm_dsj_20150527a logs]# cut -d / -f 4 test_err.log | sort
192.168.1.100
192.168.1.100
192.168.1.100
192.168.1.100
192.168.1.101
192.168.1.101
192.168.1.101
192.168.1.102
192.168.1.102
[root@vm_dsj_20150527a logs]# cut -d / -f 4 test_err.log | sort |uniq -c #对IP地址排序sort并展示重复的次数uniq -c
      4 192.168.1.100
      3 192.168.1.101
      2 192.168.1.102
[root@vm_dsj_20150527a logs]# cut -d / -f 4 test_err.log | sort |uniq -c | sort -k 1 -n -r
      4 192.168.1.100
      3 192.168.1.101
      2 192.168.1.102
[root@vm_dsj_20150527a logs]# cut -d / -f 4 test_err.log | sort |uniq -c | sort -k 1 -n -r | head -2   #取前两个ip地址
      4 192.168.1.100
      3 192.168.1.101
[root@vm_dsj_20150527a logs]# cut -d / -f 4 test_err.log | sort |uniq -c | sort -k 1 -n -r | head -10  #取前10个ip地址
      4 192.168.1.100
      3 192.168.1.101
      2 192.168.1.102
[root@vm_dsj_20150527a logs]# cut -d / -f 4 test_err.log | sort |uniq -c | sort -k 1 -n -r | awk {
          
   if ($1>3) print $0}  #取出现次数超过3次的ip地址
      4 192.168.1.100
[root@vm_dsj_20150527a logs]# cut -d / -f 4 test_err.log | sort |uniq -c | sort -k 1 -n -r | awk {
          
   if ($1>2) print $0}  #取出现次数超过2次的ip地址
      4 192.168.1.100
      3 192.168.1.101

命令说明:

    cut

This is a test line. -d 字符:指定分隔符 -f#: 指定要显示字段

    sort

按字符进行比较 sort [option] file… -f: 忽略字符大小写;-n: 比较数值大小; -t: 指定分隔符 -k: 指定分隔后进行比较字段 -u: 重复的行,只显示一次;

    uniq

移除重复的行 -c:显示每行重复的次数 -d:仅显示重复过的行 -u: 仅显示不曾重复的行

经验分享 程序员 微信小程序 职场和发展