Compare lines from /etc/group on different servers

M

mac8500

Simple script:

I run it .... server$ ./group.pl ./tmp/test.group.* > group.csv (where
* is servernames).

more ./tmp/test.group.server1

This is to output the group id's in a file readable by excel BUT i need
to find the differences between the servers.

I tought i could put my first pass threw the file and put the entire
line in a $ so i could compare the lines between them and if they don't
match just print it ... but i couldn't get that to work ... i'm sure it
easy i just can't get a grasp on a simple solution.

Thanks.

***!!!***!!!*** server1
system:0
staff:1
bin:2
sys:3
adm:4
uucp:5
mail:6
security:7
cron:8
printq:9
audit:10
nobody:4294
usr:100


#!/usr/bin/perl

my @servers;

while(<>) {
my($line) = $_;
chomp($line);
if ($line =~ /\*\*\*/) {
$currentserver = substr $line, 16;
push (@servers,$currentserver);
} else {
( $login, $userid, $gid, ) = split( ':' );
chomp($gid);
$users{ $login }{ $currentserver } = $gid;
}
}

#
# Main program.
#

print "USERNAMES";

foreach $servername (sort @servers) {
print ",".$servername
}

print "\n";

foreach $login (sort keys %users) {
print $login;
foreach $servername (sort @servers) {
if ($users{$login}{$servername} >= 0) {
print ",".$users{$login}{$servername};
} else {
print ", ";
}
}
print "\n";
}
close FILES;
 
T

Tad McClellan

print "USERNAMES";

foreach $servername (sort @servers) {
print ",".$servername
}

print "\n";


You could replace all of that with:

print join(",", "USERNAMES", sort @servers), "\n"; # untested
 
S

sbusiello

#==================================================
# this will match all or none
#==================================================
open(S1,"s1.txt") or die "Couldn't open s1.txt";
my @s1 = <S1>;
close(S1);

open(S2,"s2.txt") or die "Couldn't open s2.txt";
my @s2 = <S2>;
close(S2);

chomp(@s1); # remove newlines
chomp(@s2); # remove newlines


my $s1Str = join( ",", sort @s1 );
my $s2Str = join( ",", sort @s2 );

print $s1Str . "\n";
print $s2Str . "\n";

if ( $s1Str eq $s2Str ) {
print "We have a match\n";
}else{
print "Servers have distint listings \n";
}


#==================================================
# might want to match on an entry by entry basis
#==================================================
#
# To compare two arrays I would advise using a module
#
http://www.annocpan.org/~DAVECROSS/Array-Compare-1.13/lib/Array/Compare.pm
#
# or you can use the simple N^2 method below
#

foreach $s1val ( sort @s1 ){

foreach $s2val ( sort @s2 ){

if ( $s1val eq $s2val ){
print "We have a match: $s1val == $s2val\n";
}

}
}

-Steven
The World's Tastiest Database
http://www.iamfood.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top