【问题标题】:Parsing a log file into CSV format将日志文件解析为 CSV 格式
【发布时间】:2012-04-03 11:35:49
【问题描述】:

我正在尝试开发一小段代码来帮助我划定一个日志文件,该日志文件存储了在一周内发送到不同 IP 的大量跟踪路由。

我希望在每个跟踪路由的开头和结尾使用“--- START ---”和“--- END ---”标签来拆分它们。示例如下:

--- START ---
Mon Mar 12 22:45:05 GMT 2012
traceroute to xxxxxxx (xxxxxxxx), 30 hops max, 60 byte packets
1  xxxxxxx (xxxxx)  1.085 ms  1.662 ms  2.244 ms
2  xxxxxxxx (xxxxxxx)  0.792 ms  0.782 ms  0.772 ms
3  xxxxxxxxx (xxxxxxxxx)  8.545 ms  9.170 ms  9.644 ms
4  etc
5  etc
--- END ---
--- START ---
Mon Mar 12 22:45:05 GMT 2012
traceroute to xxxxxxxxx (xxxxxxxx), 30 hops max, 60 byte packets
1  139.222.0.1 (xxxxxxxxx)  0.925 ms  1.318 ms  1.954 ms
2  10.0.0.1 (xxxxxxxx)  0.345 ms  0.438 ms  0.496 ms
3  172.16.0.34 (xxxxxxxxx)  0.830 ms  2.553 ms  0.809 ms
4 etc
5 etc
6 etc
--- END ---

任何人都可以帮助我了解如何在 matlab 或 java 中完成此操作...我也在尝试计算每个跟踪路由的跃点数。那就是抛出 --- END --- 标签之前给出的数字...

任何帮助将不胜感激,

干杯。

【问题讨论】:

  • 只看每一行的第一个字母。如果它是大写字母:它是带有日期的第一行。如果是小写(traceroute),则为第二行。如果它是数字:它是“跳”线之一。

标签: java parsing matlab csv logging


【解决方案1】:

快速和肮脏的例子:

import java.io.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Main
{
  private static class Record
  {
    public Date date;

    public String to;
    public int hops;
    public int packetSize;

    public String toString()
    {
      return date + " ::: " + to + ", " + hops + " hops, "
        + packetSize + " bytes";
    }
  }

  public static void main(String[] args) throws Exception
  {
    Scanner s = new Scanner(new FileReader(new File("input.txt")));

    Record currentRecord = null;
    ArrayList<Record> list = new ArrayList<Record>();

    while (s.hasNextLine()) {
      String line = s.nextLine();

      if ("--- START ---".equals(line)) {
        currentRecord = new Record();

      } else if ("--- END ---".equals(line)) {
        list.add(currentRecord);
        currentRecord = null;

      } else if (currentRecord.date == null) {
        currentRecord.date = new SimpleDateFormat(
          "EEE MMM dd HH:mm:ss zzz yyyy").parse(line);

      } else if (line.startsWith("traceroute to ")) {
        Pattern p = Pattern.compile(
          "traceroute to ([^ ]+) [^,]+, ([^ ]+) hops max, ([^ ]+) byte packets");
        Matcher m = p.matcher(line);

        if (m.matches()) {
          currentRecord.to = m.group(1);
          currentRecord.hops = Integer.parseInt(m.group(2));
          currentRecord.packetSize = Integer.parseInt(m.group(3));
        }
      }
    }

    for (int i = 0; i < list.size(); i++)
      System.out.println(list.get(i).toString());
  }

}

输出:

Tue Mar 13 04:15:05 GMT+05:30 2012 ::: 203.98.69.105, 30 hops, 60 bytes
Tue Mar 13 04:15:05 GMT+05:30 2012 ::: 62.193.36.27, 30 hops, 60 bytes

我正在为您指出许多不同的方向(ScannerPatternSimpleDateFormat 等)。对于单个“网关”项,您还可以使用String.split() 使用" "(两个空格)作为分隔符。

【讨论】:

  • 感谢您的帮助。我还试图显示每条跟踪路由的跳数,我发现这很困难,因为它没有说明最后的跟踪路由。相反,它显示了“--- END ---”行之前的最后一条轨迹。我将如何告诉代码在最后一个 END 行之前找到最后一个跟踪?然后 id 就可以打印出它制作的啤酒花数量...
  • @FredBones 正如我在帖子中所述,我通过向您介绍了一系列可用于解析文本的类,为您指明了正确的方向。提示:添加另一个else if 块并解析跃点,包括数字(首先出现!);使用包含所有跃点的 ArrayList 更新 Record。我希望你能自己写出逻辑。
  • Manish,感谢您的帮助!我已经有一段时间没有使用过 java 了,只是不得不重新使用它,所以我觉得它有点困难。我会接受您的建议,并以进展情况回复您!干杯!
  • Manish,我发现很难解释您的代码中发生了什么。我试图让它读取每个跟踪路由的每一行,但不确定如何。如果您可以通过向我展示如何获得最终跳数来让我开始,那将非常感激......
猜你喜欢
  • 2011-04-26
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 2014-03-02
  • 2015-06-07
  • 1970-01-01
相关资源
最近更新 更多