用Perl解析文本文件并用JSON存储信息

用Perl解析文本文件并用JSON存储信息,json,perl,parsing,Json,Perl,Parsing,因此,我有一个文件,它包含不同文件的不同路径,以及它是什么类型的文件更改,以及行中更改的数量。像这样的 abc123: Files Changed: lines: new deleted unchanged some/path/to/file.c 15 0 4234 some/other/file.h

因此,我有一个文件,它包含不同文件的不同路径,以及它是什么类型的文件更改,以及行中更改的数量。像这样的

abc123:
  Files Changed:                             lines: new    deleted  unchanged
    some/path/to/file.c                              15      0           4234
    some/other/file.h                                 1      0            223
    some/other/path/to/file2                          3      1              3
  Files Created:                             lines: new    deleted  unchanged
    some/path/to/file.c                               3      1              3           
  Files Changed:                             lines: new    deleted  unchanged
    some/other/path/to/file                           2      2            234
我需要找到一个简单的方法来解析这个。我真的不在乎行的变化(新的,删除的,不变的)。我想要的是一个JSON。大概是这样的:

{
    "abc123":{
        "Files Changed:": [ 
            "some/path/to/file.c",
            "some/other/file.h",
            "some/other/path/to/file",
            "some/other/path/to/file2"
         ],
        "Files Created:":[
            "some/path/to/file.c"
         ]
     }
}

更难的部分是尝试解析文本文件,我想要的东西可以处理文件提供给你的任何东西。我知道任何有“/”的东西都可以工作,然后是文件字符串,但我不知道如何判断它是“文件更改”还是“文件创建”。此外,该文件可能具有类似于“已删除文件”“文件链接”的内容及其相应的文件路径。如果您能帮上忙,我们将不胜感激。

只要行开头的空格保持一致,就很容易做到这一点。你需要逐行阅读,记住你在哪一层看到了什么

在下面的代码中,我假设每个级别有两个缩进空间。因为这看起来像是某种版本控制摘要,所以我打电话给

  • 第0级缩进abc123
    $commit
  • 第1级是对下面列出的文件执行的
    $operation
  • 第二级包含文件名

如果行开头的空格是一致的,这是很容易实现的。你只需要逐行阅读,记住你最后在哪一层看到了什么。这是git输出吗?你是生命的救星!。没有考虑空间。成功了!谢谢
use strict;
use warnings;
use JSON 'to_json';

my $commit; # abc123
my $operation; # Files Changed, Files Created
my $data; # this is the target

while (my $line = <DATA>) {
    if ($line =~ /^(\S+):/) {
        # line contains a commit
        $commit = $1;
        next;
    }
    if ($line =~ /^\s\s([^:]+):/) {
        # line contains an operation
        $operation = $1;
        next;
    }
    if ($line =~ /^\s\s\s\s(\S+)/) {
        # this is a filename
        push @{ $data->{$commit}->{$operation} }, $1;
    }
}

print to_json $data;

__DATA__
abc123:
  Files Changed:                             lines: new    deleted  unchanged
    some/path/to/file.c                              15      0           4234
    some/other/file.h                                 1      0            223
    some/other/path/to/file2                          3      1              3
  Files Created:                             lines: new    deleted  unchanged
    some/path/to/file.c                               3      1              3
  Files Changed:                             lines: new    deleted  unchanged
    some/other/path/to/file                           2      2            234
{"abc123":{"Files Changed":["some/path/to/file.c","some/other/file.h","some/other/path/to/file2","some/other/path/to/file"],"Files Created":["some/path/to/file.c"]}}