简体   繁体   English

解析日志..只是无法弄清楚

[英]Parsing logs.. just can't figure it out

Alright, so I'm supposed to make a script that parses all the chat logs from irc channels, combine the ones that match names, and then sort the chat logs by date. 好吧,所以我应该编写一个脚本,以解析来自irc通道的所有聊天记录,合并与名称匹配的聊天记录,然后按日期对聊天记录进行排序。

This is an example chat log: 这是一个示例聊天日志:

    jul 29 19:20:53 <lol> lolfile3
    jul 31 19:20:53 <lol> lolfile3321
    aug 1 19:20:53 <lol> lolfile31324
    jul 30 19:20:53 <lol> lolfile32

I need to order them by the timestamp (jul 30 19:20:53) however I've been trying for hours on end but to no avail. 我需要按时间戳排序(7月30日19:20:53),但是我已经连续尝试了几个小时,但无济于事。

Here's the code I already have (this is file merging, file writing and everything. The timestamp is the last thing I need to do!) 这是我已经拥有的代码(这是文件合并,文件写入等所有操作。时间戳是我要做的最后一件事!)

I couldn't figure out how to add code to my question so I link you to pastebin: http://pastebin.com/2VrSRZZr 我不知道如何添加代码到我的问题,所以我将您链接到pastebin: http : //pastebin.com/2VrSRZZr

Thank you so much if you could help me by posting code. 非常感谢您能通过发布代码为我提供帮助。 Obviously I don't expect to be spoonfed but some code would be nice. 显然,我不希望被人spoon病,但是一些代码会很好。

I'm not entirely sure what the "" in your example is, so this may not be entirely correct, but it shows how to convert the timestamp strings to comparable objects. 我不确定您的示例中的“”是什么,因此可能并不完全正确,但是它说明了如何将时间戳记字符串转换为可比较的对象。 Hopefully this gets you on the right track. 希望这可以让你在正确的轨道上。

DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy")
List<String> lines = readLines(); // use a function of your to read the lines
List<LogEntry> entries = new LinkedList<LogEntry>();
for ( String line : lines ) {
  int splitIndex = line.indexOf("<log>");
  String time = line.substring(0,splitIndex);
  Date date = dateFormat.parse(time);
  entries.add(new LogEntry(date,line.substring(splitIndex));
}

Collections.sort(entries);


// create a class to hold the log contents and the timestamp

class LogEntry implements Comparable<LogEntry> {
    private final Date time;
    private final String entry;

    public void compare(LogEntry other) {
       return time.compareTo(other.time);
    }
}

EDIT: I ran the code you created in pastebin and the output was 编辑:我运行您在pastebin中创建的代码,并且输出是

Fri Jul 29 17:09:50 EDT 2011 <phl0w> tes
Sun Jul 31 17:08:49 EDT 2011 <yyyy> alewrwae
Sun Jul 31 17:09:50 EDT 2011 <phl0w> tes
Sun Jul 31 17:10:49 EDT 2011 <Andy_> Speed
Sun Jul 31 17:10:51 EDT 2011 <Andy_> lol Speed
Sun Jul 31 17:11:51 EDT 2011 <xxxx> wrkjaer
Sun Jul 31 19:20:50 EDT 2011 <phl0w> lolfile1
Sun Jul 31 19:20:53 EDT 2011 <phl0w> lolfile3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM