Perl输出格式

时间:2016-08-29 14:17:41

标签: perl

我正在阅读一个日志文件,并根据“程序”名称对其进行分组,然后将其分配给它。

日志文件

------------------------------------------
DEV: COM-1258
Program:Testing
Reviewer:Jackie
Description:New Entries
rev:r145201
------------------------------------------
QA: COM-9696
Program:Testing
Reviewer:Poikla
Description:Some random changes
rev:r112356
------------------------------------------
JIRA: COM-1234
Program:Development
Reviewer:John Wick
Description:Genral fix
rev:r345676
------------------------------------------
JIRA:COM-1234
Program:Development
Reviewer:None
Description:Updating Received
rev:r909276
------------------------------------------
JIRA: COM-6789
Program:Testing
Reviewer:Balise Mat
Description:Audited
rev:r876391
------------------------------------------
JIRA: COM-8585
Program:Testing
Reviewer:Gold frt
Description: yet to be reviewed
rev:r565639

我的代码,

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Terse    = 1;

my $file = "log.txt";
open FH, $file or die "Couldn't open file: [$!]\n";

my $data = {};
my $hash = {};
while (<FH>)
{
    my $line = $_;
    chomp $line;
    if ($line =~ m/(-){2,}/)
    {
        my $program = $hash->{Program} || '';
        my $jira    = $hash->{JIRA} || $hash->{QA} || $hash->{DEV} ||
+'';
        if ($program && $jira)
        {
            push @{$data->{$program}{$jira}}, $hash;
            $hash = {};
        }
    }
    else
    {
        if ($line =~ m/:/)
        {
            my ($key, $value) = split /:\s*/, $line;
            $hash->{$key} = $value;
        }
        elsif ($line =~ m#/# && exists $hash->{Files})
        {
            $hash->{Files} .= "\n$line";
        }
    }
}
print 'data = ' . Dumper($data);

foreach my $prg (sort keys %{$data})
{
    print "===========================================================
+=\n";
    print "    PROGRAM : $prg
+         \n";
    print "===========================================================
+=\n";

    foreach my $jira (sort keys %{$data->{$prg}})
    {
        print "******************\n";
        print "JIRA ID : $jira\n";
        print "******************\n";

        foreach my $hash (@{$data->{$prg}{$jira}})
        {
            foreach my $key (keys %{$hash})
            {
                # print the data except Program and JIRA
                next if $key =~ m/(Program|JIRA|DEV|QA)/;
                print "    $key => $hash->{$key}\n";
            }
            print "\n";
        }
    }
}

我要求以以下格式打印输出,目前我的逻辑无法这样做,任何想法都会非常有用。

PROGRAM:  Development
Change IDs:
1.JIRA
    a.COM-1234

PROGRAM:  Testing
Change IDs:
1.JIRA
    a.COM-6789
    b.COM-8585
2.QA
    a.COM-9696
3.DEV
    a.COM-1258

2 个答案:

答案 0 :(得分:1)

我会写这个

PROGRAM:   Development
Change IDs:
1.JIRA
    a.COM-1234

PROGRAM:   Testing
Change IDs:
1.JIRA
    a.COM-6789
    b.COM-8585
2.QA
    a.COM-9696
3.DEV
    a.COM-1258

输出

@A1
ABCGKJTGE
+
A4
@B1
ACDFS
+
B4
@C1
SFDGDGDAD
+
C4

答案 1 :(得分:0)

#!/usr/bin/perl -w

use strict;
use warnings;

use Data::Dumper;

my $file = 'test';
my $hash;
my $id_hash = ();
my $line_found = 0;
my $line_count = 1;
my $ID;
my $ID_num;

open (my $FH, '<', "$file") or warn $!;

while (my $line = <$FH> ) {

    chomp($line);

    if ( $line =~ m/------------------------------------------/){
        $line_found = 1;
        $line_count++;
        next;
    }

    if ( $line_found ) {
        $line =~ m/(.*?):(.*)/;
        $ID = $1;
        $ID_num = $2;
        $line_found = 0;
    }

    if ( $line =~ m/Program:(.*)/ ) {
        my $pro = $1;
        push @{$hash->{$pro}->{$ID}}, ($ID_num) ;
    }

    $line_count++;
}

close $FH;

foreach my $pro (keys %$hash){
    # print Dumper($pro);

    print "PROGRAM:\t$pro\nChange IDs:\n";

    foreach my $ids (keys $hash->{$pro}){

        print "\t1. $ids\n";

        foreach my $id (values $hash->{$pro}->{$ids}){
            print "\t\ta. $id\n";
        }
    }
}

输出

PROGRAM:        Testing
Change IDs:
        1. QA
                a.  COM-9696
        1. DEV
                a.  COM-1258
        1. JIRA
                a.  COM-6789
                a.  COM-8585
PROGRAM:        Development
Change IDs:
        1. JIRA
                a. COM-1234
                a. COM-1234

只需根据需要更改输出!!