-1

could you please help me how to get unique keys with different values in perl?, these log files are frequently generating and values are keep on changing.

"cloning": true
"cmdline": "git-upload-pack
"features": ""
"frontend": "github.com"
"frontend_pid": 14421
"frontend_ppid": 1
"git_dir": "/repositories/xorg/myrepo.git"
"hostname": "github.com"
"pgroup": "20603"
"pid": 20603
"ppid": 20600
"program": "upload-pack"

"cloning": false
"cmdline": "git-upload-pack
"features": ""
"frontend": "github.com"
"frontend_pid": 14422
"frontend_ppid": 2
"git_dir": "/repositories/yorg/myrepo2.git"
"hostname": "github.com"
"pgroup": "20604"
"pid": 20604
"ppid": 20500
"program": "upload-pack"

Thanks in Advance

clt60
  • 62,119
  • 17
  • 107
  • 194
coolent
  • 21
  • 10
  • 1
    Please show us what you have tried so far, and also see [how to ask a good question on Stack Overflow](http://stackoverflow.com/help/how-to-ask). – i alarmed alien Sep 25 '14 at 09:29
  • See also this question: http://stackoverflow.com/questions/17218110/can-a-hash-key-have-multiple-subvalues-in-perl – i alarmed alien Sep 25 '14 at 09:32
  • This is not a Perl hash. It looks more like a JSON object, but the commas are missing. Or is this part of the log file? – simbabque Sep 25 '14 at 09:33
  • hi this is part github audit log i am trying to get unique key with multiple values. I am not able to copy my code here , so far I done, I'll try again, Thanks for everyone help – coolent Sep 25 '14 at 09:48

2 Answers2

0
use Data::Dump;

my %h;
while (my $line = <DATA>) {
  next if $line !~ /\S/;

  my @r = split /:/, $line;
  s/^["\s]+ | ["\s]+//xg for @r;

  push @{ $h{$r[0]} }, $r[1];
}

dd \%h;

__DATA__
"cloning": true
"cmdline": "git-upload-pack
"features": ""
"frontend": "github.com"
"frontend_pid": 14421
"frontend_ppid": 1
"git_dir": "/repositories/xorg/myrepo.git"
"hostname": "github.com"
"pgroup": "20603"
"pid": 20603
"ppid": 20600
"program": "upload-pack"

"cloning": false
"cmdline": "git-upload-pack
"features": ""
"frontend": "github.com"
"frontend_pid": 14422
"frontend_ppid": 2
"git_dir": "/repositories/yorg/myrepo2.git"
"hostname": "github.com"
"pgroup": "20604"
"pid": 20604
"ppid": 20500
"program": "upload-pack"

output

{
  cloning       => ["true", "false"],
  cmdline       => ["git-upload-pack", "git-upload-pack"],
  features      => ["", ""],
  frontend      => ["github.com", "github.com"],
  frontend_pid  => [14421, 14422],
  frontend_ppid => [1, 2],
  git_dir       => [
                     "/repositories/xorg/myrepo.git",
                     "/repositories/yorg/myrepo2.git",
                   ],
  hostname      => ["github.com", "github.com"],
  pgroup        => [20603, 20604],
  pid           => [20603, 20604],
  ppid          => [20600, 20500],
  program       => ["upload-pack", "upload-pack"],
}
mpapec
  • 50,217
  • 8
  • 67
  • 127
0

I would just use a hash of arrays:

#!/usr/bin/env perl 
use strict;
my %hash;
## read input line by line into $_
while (<>) {
    ## remove trailing newlines
    chomp;
    ## skip empty lines
    next if /^\s*$/;
    ## split the line on `:` into the @F array
    my @F=split(/:\s*/);
    ## Add this value to the list associated with 
    ## this key.
    push @{$hash{$F[0]}},$F[1];
}
## Set the output field separator to a comma
$"=",";
## Print each key and the array of its values separated
## by a comma.
print "$_ : ", join(",",@{$hash{$_}}),"\n" for keys(%hash)
terdon
  • 3,260
  • 5
  • 33
  • 57