[英]how can i extract host name and occurence of host request from .txt file in java 7?
How can I find the host name and the number of requests from the same host using regex and hashmap for the below input text file:如何使用以下输入文本文件的正则表达式和哈希图找到来自同一主机的主机名和请求数:
input.txt输入文件
unicomp6.unicompt.net - - [01/JUL/1995:00:00:06 - 0400] "GET /shuttle/countdown/ HTTP/1.0" 200 3985
burger.letters.com - - [01/JUL/1995:00:00:12 - 0400] "GET /shuttle/countdown/ HTTP/1.0" 200 0
d104.aa.net - - [01/JUL/1995:00:00:13 - 0400] "GET /shuttle/countdown/ HTTP/1.0" 200 3985
unicomp6.unicompt.net - - [01/JUL/1995:00:00:14 - 0400] "GET /shuttle/countdown/ HTTP/1.0" 200 40310
d104.aa.net - - [01/JUL/1995:00:00:15 - 0400] "GET /shuttle/countdown/ HTTP/1.0" 200 40310
d104.aa.net - - [01/JUL/1995:00:00:15 - 0400] "GET /images/NASA-logosmall.gif HTTP/1.0" 200 786
unicomp6.unicompt.net - - [01/JUL/1995:00:00:14 - 0400] "GET /shuttle/countdown/ HTTP/1.0" 200 786
unicomp6.unicompt.net - - [01/JUL/1995:00:00:14 - 0400] "GET /shuttle/countdown/ HTTP/1.0" 200 1204
desired output:所需的输出:
unicomp6.unicompt.net 4
burger.letters.com 1
d104.aa.net 3
Why not using a regex ?为什么不使用正则表达式?
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\w+\\.\\w+\\.\\w+", Pattern.DOTALL);
String input = "unicomp6.unicompt.net - - [01/JUL/1995:00:00:06 - 0400]"+
"burger.letters.com - - [01/JUL/1995:00:00:12 - 0400] .... etc";
Matcher m = pattern.matcher(input);
while (m.find()) {
String s = m.group();
System.out.println(s);
}
}
you can try this (for Java 8 and beyond):你可以试试这个(对于 Java 8 及更高版本):
public static void main(String[] params) throws IOException {
try (Stream<String> lines = Files.lines(Paths.get("src/main/resources/input.txt"))) {
Map<String, Integer> occurrences = new HashMap<>();
lines.map( line -> line.split(" ") )
.forEach( splitted -> {
occurrences.merge(splitted[0], 1, Integer::sum);
} );
System.out.print( occurrences );
}
}
just be careful for path of your txt file小心你的txt文件的路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.