读取文本文件的第一列并输出到新的文本文件中(perl语言) 5
1个回答
展开全部
#!perl
use strict;
use warnings;
use autodie qw{open close};
sub main;
sub get_id_info;
sub extract;
sub tell_account_type_by_uid;
main();
sub main {
my $id_info_map = get_id_info();
extract($id_info_map);
}
sub get_id_info {
my $id_info_map = +{};
my $file = 'password.old.txt';
open my $in_fh, '<', $file;
while (defined(my $line = readline $in_fh)) {
my @parts = split /:/, $line;
my $id = $parts[0];
my $uid = $parts[2];
my $shell = $parts[-1];
$id_info_map->{$id}->{uid} = $uid;
$id_info_map->{$id}->{shell} = $shell;
}
close $in_fh;
return $id_info_map;
}
sub extract {
my ($id_info_map) = @_;
my $account_type_file = 'account_type.txt';
open my $out_acct_type_fh, '>', $account_type_file;
my $shell_type_file = 'shell_type.txt';
open my $out_shell_type_fh, '>', $shell_type_file;
my @keys_sort_by_uid = sort {
$id_info_map->{$a}->{uid} <=> $id_info_map->{$b}->{uid}
} keys %{$id_info_map};
for my $id (@keys_sort_by_uid) {
my $uid = $id_info_map->{$id}->{uid};
my $shell = $id_info_map->{$id}->{shell};
my $account_type = tell_account_type_by_uid($uid);
my $acct_type_line = "$account_type: $id$/";
print {$out_acct_type_fh} $acct_type_line;
my $shell_type_line = "User id: $id, Shell value: $shell$/";
print {$out_shell_type_fh} $shell_type_line;
}
close $out_acct_type_fh;
close $out_shell_type_fh;
print "Done extracting, cost @{[time - $^T]} seconds. " .
"Output saved to $account_type_file and $shell_type_file$/";
}
sub tell_account_type_by_uid {
my ($uid) = @_;
# better check /etc/login.defs for UID_MIN and UID_MAX
my $account_type = q{};
if ($uid == 0) {
$account_type = 'Super User';
}
elsif ($uid <= 500) {
$account_type = 'System Type';
}
else {
$account_type = 'Regular Account';
}
return $account_type;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询