tux242 has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I am trying to take the code below and make it so that if it does not find the user $login instead of just ignoring it, it will put a null or X if you will, if it is not found in that particular /etc/passwd file onto the end of the line. The output should look something like this:
Instead of like this:waj4356:server1:server2:X:server4:server5:X:X:server8:server9 zaw8732:X:server2:server3:server4:X:server6:X:server8:X cvf6609:X:X:X:X:server5:server6:server7:X:X
So that when I split via : delimited I have a standardized type form of 9 servers, X's for the ones the ID is not present in and the server name for the ones in which it is. Instead of using if exists maybe an elseif or other loop type condition would be better, I would appreciate any suggestions. Thanks Againwaj4356:server1:server2:server4:server5:server8:server9 zaw8732:server2:server3:server4:server6:server8 cvf6609:server5:server6:server7
#!/usr/bin/perl -w use strict; my %USERS; foreach my $file (<passwds.*>) { open(PASSWD,$file); my $strip=$file; #strips passwd. from passwd.server $strip =~ s/passwds\.//; while(<PASSWD>) { my($login,$gcos) = (split(':',$_))[0,4]; if(exists $USERS{$login}) { push(@{$USERS{$login}},$strip); } else { $USERS{$login} = [$strip]; } } close(PASSWD); } open(NEWFILE,">file3") || print "Can't open file3: $!\n"; foreach my $login (sort keys %USERS) { print NEWFILE "$login:".shift(@{$USERS{$login}}).":"; print NEWFILE join(':',@{$USERS{$login}})."\n"; } close(NEWFILE); print "the Active Servers listing is done\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
How to ask for help
by Limbic~Region (Chancellor) on Nov 24, 2003 at 17:02 UTC | |
|
Re: Standardized Template
by dragonchild (Archbishop) on Nov 24, 2003 at 15:52 UTC | |
|
Re: Standardized Template
by Roy Johnson (Monsignor) on Nov 24, 2003 at 16:47 UTC |