1753774 Members
6951 Online
108799 Solutions
New Discussion юеВ

script help

 
JimJG
New Member

script help

I have a flat file called input:

user|groupm groupb groupc goupa
userb|groupz group1d groupk

And I have a command string:

command user # command with ran against user
groupc groupa groupq groupb # output of command

what I need to do is
1. compare the input file with the output of the command and check to see if the groups are different and echo out OK if they are all the same or in sync or NOT OK if they are not in sync. And of course give me the results.

the input file can have 10 to 1000 lines. And the groups are not in any order from the command or in the input file.

Thanks
Jim
2 REPLIES 2
Hein van den Heuvel
Honored Contributor

Re: script help


If I had to do this through shell stuff only, then I'd take both target line and convert each to a file with a line per group.
Sort those, and diff.


But of course I would not solve it with a shell script but with perl. I've done something similar to your question before, and adapted it to do just what you want and then some.
Of course you may want to change the final return handling, and remove debug & detail printing.

---- input ----
user|group groupb groupc groupa
userb|groupk group1d groupz
---- tmp.txt ----
usera|groupm groupb groupc goupa
userb|groupz group1d groupk
user|groupa group groupX goupa
userc|groupz group1d groupkuser
---- command -----
#perl -ne "print if /user\|/" tmp.txt
user|groupa group groupX goupa

---- tmp.pl with perl solution (also attached) ---
$user = shift @ARGV or die "Must provide username";
$input = shift @ARGV or die "Must provide input file name";
open INPUT, "<$input" or die "Could not read file $input";
while () {
chomp;
$input_groups = $' if /^$user\|\s*/;
}

if ($_ = $input_groups) {
foreach $group (split) {
$input{$group}++;
}

$_ = `perl -ne "print \$' if /^$user\\|\s*/" tmp.txt`;
chomp;
print "-- $input_groups -- $_ --\n";

foreach $group (split) {
if ($input{$group}) {
delete $input{$group};
} else {
print "group $group for $user not found in $input\n";
$not = "NOT";
}
}
foreach $group (keys %input) {
print "group $group for $user not found in through command\n";
$not = "NOT";
}
print "${not}OK\n";
} else {
die "user $user not found"
}


----- usage samples -----
#perl tmp.pl userb input.txt
-- groupk group1d groupz -- groupz group1d groupk --
OK

#perl tmp.pl user input.txt
-- group groupb groupc groupa -- groupa group groupX goupa --
group groupX for user not found in input.txt
group goupa for user not found in input.txt
group groupb for user not found in through command
group groupc for user not found in through command
NOTOK

---------------
Enjoy,
Hein.
Hein van den Heuvel
Honored Contributor

Re: script help

So Jim... did that work for you? Hein.