简体   繁体   English

我如何从 java 7 中的 .txt 文件中提取主机名和主机请求的出现?

[英]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.

相关问题 如何从Java中的txt文件提取以下信息 - How can I extract the following information from a txt file in java Java 正则表达式从 URL 中提取主机名和域名 - Java regex to extract host name and domain name from a URL 如何在Java中检查系统IP地址/主机名? - How can i check System IP Address/Host Name in Java? 如何从Java中的主机文件获取域名? - How to get the domain name from host file in java? 如何计算Java中txt文件中某个特定单词的出现? - How do I count an occurence of a specific word in a txt file in Java? 如何使用 java 从 txt 文件中提取特定信息? - How can I extract specific info from a txt file using java? 我如何在Java中获取客户端系统的用户名和域名,因为我可以使用request.getRemoteHost获得IP地址和主机名 - How can i get the username and domain name of client system in java as i can get the ip address and host name using request.getRemoteHost 如何从CORBA IOR和NamingContextExt中提取服务器Host / IP? [Java] - How to extract the server Host/IP from the CORBA IOR & NamingContextExt? [Java] Java代理:如何从HttpRequest中提取目标主机和端口? - Java Proxy: How to extract Destination Host and Port from the HttpRequest? 如何从 http 或 https 请求获取带有端口的主机名 - How to get host name with port from a http or https request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM