统计日志中ip访问次数并排序的三种方法
生活随笔
收集整理的這篇文章主要介紹了
统计日志中ip访问次数并排序的三种方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1? 利用linux中的awk命令
grep "GET aaa.log?| awk -F " " '{print $NF}' >d:/test.log假設(shè)日志的最后一行是ip地址,則取出日志的最后一行以空格為分隔符并且重定向到d盤下的test.log中。
取出之后還可以進(jìn)一步操作直接統(tǒng)計出ip訪問的次數(shù)并且降序排列。
cat test.log | sed 's/:[0-9]\+//g'| awk '{IP[$1]++}END {for(a in IP) print a"-----"IP[a]}' |sort -r > d:/test1.log2 提取到ip后,可以利用excel對ip地址對訪問次數(shù)進(jìn)行統(tǒng)計(數(shù)據(jù)透視表解決方案)
此小節(jié)的數(shù)據(jù)透視表解決方案轉(zhuǎn)自https://blog.51cto.com/crazysmogu/2051735
?(1)按照默認(rèn)設(shè)置,在新工作表中創(chuàng)建數(shù)據(jù)透視表,點擊【確定】按鈕。
?
(2)在新出現(xiàn)的透視表區(qū)域,用鼠標(biāo)單擊任何一個單元格。然后把【姓名】字段分別拖到【行標(biāo)簽】和【數(shù)值】兩個方框中去。
(3)這樣,透視表中行標(biāo)簽就顯示了A列不重復(fù)姓名,后面一列跟隨的就是出現(xiàn)的次數(shù)。?
?
?
3編寫腳本進(jìn)行提取(利用map)
import java.io.*; import java.util.*;public class ipCount{public static void main(String[] args) throws IOException {HashMap<String,Integer> hash=new HashMap<String,Integer>();Set<String> keySet=hash.keySet();File file = new File("D:/ip.txt");try (FileInputStream stream = new FileInputStream(file);InputStreamReader streamReader = new InputStreamReader(stream);BufferedReader reader = new BufferedReader(streamReader) ) {String line = "";int count=1;while((line = reader.readLine()) != null) {if (keySet.contains(line)) {count=(int)hash.get(line)+1;hash.put(line,count);} else {hash.put(line,1);}}List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(hash.entrySet());Collections.sort(list,new Comparator<Map.Entry<String,Integer > >() {public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) {return o2.getValue().compareTo(o1.getValue());}});for(Map.Entry<String,Integer> mapping:list){System.out.println(mapping.getKey() + ":" + mapping.getValue());}}} }?歡迎轉(zhuǎn)載:轉(zhuǎn)載請注明出處。
?
總結(jié)
以上是生活随笔為你收集整理的统计日志中ip访问次数并排序的三种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: abstract类中不可以有privat
- 下一篇: 显式接口成员实现你知道吗??