解析日志文件并删除不需要的数据

时间:2017-07-11 15:06:12

标签: java regex csv pattern-matching

所以我有一个日志文件,提供有关锁定和解锁节点的详细信息。每个节点的时间和日期18:02:21以及ID "ieatrcxb4498-1"

150: 2017-06-14 18:02:21 INFO  monitorinfo                    :     Info: Lock VCS on node "ieatrcxb4498-1"
     264: 2017-06-14 18:02:21 INFO  monitorinfo                   :     Info: Unlock VCS on node "ieatrcxb4498-1"
     268: 2017-06-14 18:02:21 INFO  monitorinfo                   :     Info: Lock VCS on node "ieatrcxb4496-1"
     382: 2017-06-14 18:02:21 INFO  monitorinfo                   :     Info: Unlock VCS on node "ieatrcxb4496-1"
     407: 2017-06-14 18:02:21 INFO  monitorinfo                   :     Info: Lock VCS on node "ieatrcxb4500-1"
     473: 2017-06-14 18:02:21 INFO  monitorinfo                   :     Info: Unlock VCS on node "ieatrcxb4500-1"

我想要做的是通过我的程序运行日志文件,删除不需要的数据,并将输出格式化为外观或类似的内容。

Node            Lock        Unlock  
ieatrcxb4498-1  18:02:21                    
ieatrcxb4498-1              18:02:21                
ieatrcxb4500-1  18:02:21        
ieatrcxb4500-1              18:02:21

我有一个基本程序,我发现here并进行了调整,但我正在努力让它正常工作。

public class Main {

    private static class Record
    {

        public String node;
        public Date lock;
        public Date unlock;

        @Override
        public String toString() {
            return "Record{" +
                    ", node='" + node + '\'' +
                    ", lock='" + lock + '\'' +
                    ", unlock='" + unlock + '\'' +
                    '}';
        }

    }

    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 ("SHOW_PLAN BEGIN".equals(line)) {
                currentRecord = new Record();

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

            } else if (currentRecord.lock == null) {
                currentRecord.lock = new SimpleDateFormat(
                        "HH:mm:ss").parse(line);

            }else if(currentRecord.unlock == null){
                currentRecord.unlock = new SimpleDateFormat(
                        "HH:mm:ss").parse(line);
            }
            else if (line.startsWith("traceroute to ")) {
                Pattern p = Pattern.compile(
                        "monitor ([^ ]+) [^,]+, ([^ ]+) hops max, ([^ ]+) byte packets");
                Matcher m = p.matcher(line);

                if (m.matches()) {
                    currentRecord.node = m.group(1);
//                    currentRecord.lock = m.
//                    currentRecord.unlock = m.group(3);
                }
            }
        }

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

1 个答案:

答案 0 :(得分:0)

在开始创建Record对象之前,我会在所有空格周围分割线条。我建议使用3个变量,时间,状态(锁定/解锁)及其ID来创建对象。

RelativeLayoutHeader.Bottom = 15 //don't know what happens, nothing apparently

RelativeLayoutHeader.layoutParams.addRule(...) //layoutParams does not contain a def for...)

FrameLayout.LayoutParams imgViewParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WrapContent, FrameLayout.LayoutParams.WrapContent, 0.0f);
    imgViewParams.SetMargins(10, 10, 10, 10); //does not work because i have a coordinatorLayout.

然后你可以简单地拆开空间并获取包含所需信息的某些索引,如此

public class Record
{
    String time;
    String state;
    String ID;
}

剩下要做的就是格式化输出,可以使用sprintf函数轻松完成输出

    String line = "150: 2017-06-14 18:02:21 INFO  monitorinfo   :    Info: Lock VCS on node \"ieatrcxb4498-1\"";

    String[] pieces = line.split("\\s+");

    Record obj = new Record();
    obj.time = pieces[2];
    obj.state = pieces[7];
    obj.id = pieces[11].replaceAll("\"", "");

现在,您只需遍历列表中的所有对象并检查对象的状态

System.out.printf("%-10s %-10s %-10s\n", "Node", "Lock", "Unlock");