【问题标题】:Parsing text file in Perl and store information in JSON在 Perl 中解析文本文件并在 JSON 中存储信息
【发布时间】:2016-08-06 06:09:36
【问题描述】:

所以,我有这个文件,其中包含不同文件的不同路径以及文件更改的类型以及行中的更改次数。像这样的

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"
         ]
     }
}

更难的部分是尝试解析文本文件,我想要一些可以与文件提供给你的任何东西一起使用的东西。我确定可能有用的是任何具有“/”然后是文件字符串的东西,但我不知道如何判断它是“文件已更改”还是“文件已创建”。此外,该文件可能具有类似“已删除文件”“文件链接”及其相应文件路径的内容。对于如何实现这一点的任何帮助将不胜感激。

【问题讨论】:

  • 如果行首的空格是一致的,这很容易实现。您只需要逐行阅读并记住您最后在哪个级别看到的内容。这是git输出吗?

标签: json perl parsing


【解决方案1】:

只要行首的空格一致,这很容易实现。您需要逐行阅读并记住您在哪个级别看到的内容。

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

  • 第0级缩进abc123$commit,
  • 和第一级$operation 已对下面列出的文件进行了处理。
  • 第二级包含文件名。
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"]}}

【讨论】:

  • 你是救世主!没有考虑空间。有效!谢谢!
猜你喜欢
  • 2015-06-07
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 2015-08-01
  • 2015-04-08
  • 2012-12-22
相关资源
最近更新 更多