voltas has asked for the wisdom of the Perl Monks concerning the following question:

I'm reading a log file and grouping it based on the 'Program' name. LOG FILE
------------------------------------------ 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
The code I have, (with the help of one of an esteemed perl monk :))
#!/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"; } } }
I have a requirement to print the output in the below format and currently unable to do so, any ideas would be really helpful.Thanks. btw I'm very new to perl , so pls. excuse me if its a blunt question. EXPECTED OUTPUT is below,
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

Replies are listed 'Best First'.
Re: Perl output format
by Anonymous Monk on Aug 29, 2016 at 17:52 UTC

    Not sure about order of items, but:

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1170708 use strict; use warnings; $/ = "-----\n"; my %programs; while(<DATA>) { /^(\S+): (\S+)\nProgram:(\S+)/m and $programs{$3}{$1}{$2} = 1; } #use YAML; print Dump \%programs; for my $program (sort keys %programs) { print "\nPROGRAM: $program\nChangeIDs:\n"; my $numbers = 1; for my $foo ( sort keys %{ $programs{$program} } ) { print "$numbers.$foo\n"; $numbers++; my $letters = 'a'; for my $bar ( sort keys %{ $programs{$program}{$foo} } ) { print " $letters.$bar\n"; $letters++; } } } __DATA__ ------------------------------------------ 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
      Many thnks, made some modification to suit my O/P. thnks