java - counting unique occurences of string in document -


i reading logfile java. each line in logfile, checking see if line contains ip address. if line contains ip address, want +1 count of number of times ip address showed in log file. how can accomplish in java?

the code below extracts ip address each line contains ip address, process counting occurrences of ip addresses not work.

void read(string filename) throws ioexception {     bufferedreader br = new bufferedreader(new inputstreamreader(new fileinputstream(filename)));     int counter = 0;     arraylist<ipholder> ips = new arraylist<ipholder>();     try {         string line;         while ((line = br.readline()) != null) {             if(!getip(line).equals("0.0.0.0")){                 if(ips.size()==0){                     ipholder newip = new ipholder();                     newip.setip(getip(line));                     newip.setcount(0);                     ips.add(newip);                 }                 for(int j=0;j<ips.size();j++){                     if(ips.get(j).getip().equals(getip(line))){                         ips.get(j).setcount(ips.get(j).getcount()+1);                     }else{                         ipholder newip = new ipholder();                         newip.setip(getip(line));                         newip.setcount(0);                         ips.add(newip);                     }                 }                 if(counter % 1000 == 0){system.out.println(counter+", "+ips.size());}                 counter+=1;             }         }     } {br.close();}     for(int k=0;k<ips.size();k++){         system.out.println("ip, count: "+ips.get(k).getip()+" , "+ips.get(k).getcount());     } }  public string getip(string ipstring){//extracts ip string if string contains ip     string ipaddress_pattern =      "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";      pattern pattern = pattern.compile(ipaddress_pattern);     matcher matcher = pattern.matcher(ipstring);     if (matcher.find()) {         return matcher.group();     }     else{         return "0.0.0.0";     } } 

the holder class is:

public class ipholder {      private string ip;     private int count;      public string getip(){return ip;}     public void setip(string i){ip=i;}      public int getcount(){return count;}     public void setcount(int ct){count=ct;} } 

the key word search hashmap in case. hashmap list of key value pairs (in case pairs of ips , count).

"192.168.1.12" - 12 "192.168.1.13" - 17 "192.168.1.14" - 9 

and on. easier use , access iterate on array of container objects find out whether there container ip or not.

bufferedreader br = new bufferedreader(new inputstreamreader(new fileinputstream(/*your file */)));  hashmap<string, integer> occurrences = new hashmap<string, integer>();  string line = null;  while( (line = br.readline()) != null) {      // iterate on lines , search ip address patterns     string[] addressesfoundinline = ...;       for(string ip: addressesfoundinline ) {          // did have address in file earlier? if yes, increase counter          if(occurrences.containskey(ip))             occurrences.put(ip, occurrences.get(ip)+1);          // if not, create new entry address         else             occurrences.put(ip, 1);     }  }   // treemaps automatically orered if elements implement 'comparable' case strings , integers treemap<integer, arraylist<string>> turnedaround = new treemap<integer, arraylist<string>>();  set<entry<string, integer>> es = occurrences.entryset();  // switch keys , values of hashmap , create new treemap (in case there 2 ips same count, add them list) for(entry<string, integer> en: es) {      if(turnedaround.containskey(en.getvalue()))                  turnedaround.get(en.getvalue()).add((string) en.getkey());     else {         arraylist<string> ips = new arraylist<string>();         ips.add(en.getkey());         turnedaround.put(en.getvalue(), ips);     }  }  // print out values (if there 2 ips same counts printed out without special order, require sorting step) for(entry<integer, arraylist<string>> entry: turnedaround.entryset()) {              for(string s: entry.getvalue())         system.out.println(s + " - " + entry.getkey());          } 

in case output following:

192.168.1.19 - 4 192.168.1.18 - 7 192.168.1.27 - 19 192.168.1.13 - 19 192.168.1.12 - 28 

i answered this question half hour ago , guess searching for, if need example code, take @ it.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -