请问如何在perl中获取具有不同值的唯一键?这些日志文件经常生成,值不断变化。
"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"
先谢谢
答案 0 :(得分: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"
输出
{
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"],
}
答案 1 :(得分:0)
我只想使用数组哈希:
#!/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)