Regex “我如何找到全部”;姓;在/etc/passwd文件中,5-10个用户共享哪些内容?
到目前为止,我有以下代码:Regex “我如何找到全部”;姓;在/etc/passwd文件中,5-10个用户共享哪些内容?,regex,perl,match,etcpasswd,Regex,Perl,Match,Etcpasswd,到目前为止,我有以下代码: #!/use/bin/perl -w $filename = '/etc/passwd'; open(FILE, $filename) or die "Could not read from $filename, program halting."; while (<FILE>) { chomp; ($username, $password, $uid, $gid, $uname, $home, $shell) = split(':',
#!/use/bin/perl -w
$filename = '/etc/passwd';
open(FILE, $filename) or die "Could not read from $filename, program halting.";
while (<FILE>) {
chomp;
($username, $password, $uid, $gid, $uname, $home, $shell) = split(':', $_);
my ($lastname, $firstname) = split(/ /, $uname, 2);
}
close FILE;
#/使用/bin/perl-w
$filename='/etc/passwd';
打开(文件,$filename)或死亡“无法读取$filename,程序停止。”;
而(){
咀嚼;
($username、$password、$uid、$gid、$uname、$home、$shell)=拆分(“:”,$);
我的($lastname,$firstname)=拆分(//,$uname,2);
}
关闭文件;
但现在我被下一步该做什么所困扰。当时我的rational是在do a match上拆分/etc/passwd文件上的7个值,以查看有多少$lastnames有5-10个匹配项,但是我不知道与它们关联的$username是什么
对于我的最终输出,我需要有一个由5-10人共享的姓氏列表,因此示例输出类似于:
史密斯:约翰·萨曼莎·贾雷德57龙X349 6龙/usr/bin/perl
#!/usr/bin/perl
#always use strict. use warnings
use strict;
use warnings;
use autodie;
my $filename = '/etc/passwd';
#use lexical file handle, don't need to explicitly close
#3 argument form of open. `use autodie;` takes care of error checking.
open my $fh, '<', $filename;
#users will hold key value pairs. Key = lastname, Values: arrayref of usernames
my %users;
while (<$fh>) {
chomp;
my ($username, $uname) = (split ':')[0, 4]; #Got rid of other data, not using it
my $lastname = (split ' ', $uname)[0]; #Only need the lastname
push @{ $users{$lastname} }, $username;
}
#Get keys of lastnames that are shared by 5 or more people
my @five_plus = grep { @{ $users{$_} } >= 5 } keys %users;
#So you can see the data
for my $key(@five_plus){
print "$key:\n";
print "\t$_\n" for(@{ $users{$key} });
}
#始终使用严格的。使用警告
严格使用;
使用警告;
使用自动模具;
我的$filename='/etc/passwd';
#使用词法文件句柄,不需要显式关闭
#3开放式的参数形式`使用autodie;`负责错误检查。
打开我的$fh,'提示:使用一个以姓氏为键、以用户ID列表为值的散列。对于每个姓氏,在散列中查找它并将其添加到列表中。^姓氏作为键并引用一个userid数组作为值我不擅长perl,但这里有一行代码可以帮助您:
sed-n's/^[^:::*\([^:::*\)./\1/p'/etc/passwd | sort | uniq-c | sed-n'/*[0-9]\{2,\}/{s/*(\$)/\1${x;y/\n/;s/^/;p}'
在获取lastnames键的行中,如何将其限制为最多10?所以它至少是5,最多是10。我已经尝试添加@user3735891,尝试用grep{my$t=@{$users{$\u}};($t>=5&&&$t)替换grep