Operating System - HP-UX
1821980 Members
3099 Online
109638 Solutions
New Discussion юеВ

Help required Unix script to compare group file.

 
SOLVED
Go to solution
sreeram n
Frequent Advisor

Help required Unix script to compare group file.



Hi Gurus,
Could you please help me to compare groups files of two diffrent servers in hp-ux as we sre doing server migration

group file 1
==============
root:0:user1,user2,user3
adm:1:userx,usery


group file 2
=============
root:0:user1,user3,user4,user5,user6,user7
adm:1:userx,usery,userz


final group
=============
root:0:user1,user2,user3,user4,user5,user6,user7
adm:1:userx,usery,userz

my requirement is to generate the group file something similar to the file "final group"
please send me a script that can do this job.

Thanks.

Sreeram
Sreeram N
11 REPLIES 11
Steven Schweda
Honored Contributor

Re: Help required Unix script to compare group file.

> please send me a script that can do this job.

Please send me money.
Laurent Menase
Honored Contributor

Re: Help required Unix script to compare group file.

Hi
First I would generate 2 intermediate files
root 0 user1
root 0 user2
root 0 user3
adm 1 userx
adm 1 usery


then to compare:
diff gr.intermediate0 gr.intermediate1
to merge
sort -u gr.intermediate0 gr.intermediate1 >gr.intermediate.out

script to generate intermediate file
#!/usr/bin/ksh

while read in
do
n1="${in%%:*}"
n2="${in%:*}"
n2="${in#*:}"
users="${in##*:}"
while [ ! -z "$users" ]
do
echo "$n1" "$n2" "${users%%,*}"
newusers="${users#*,}"
if [ "$newusers" = "$users" ]
then
break
fi
done
done
.....

to use: generateintermediate/tmp/gr.intermediate1
now to regenerate the file after merge with sort or diff:
#!/usr/bin/ksh
N=""
while read a b c
do
if [ "$N" != "$a" ]
then
echo "\n$a:$b:$c\c"
$N="$a"
else
echo ",$c\c"
fi
done
echo "\n"
.....
to regenerate a group file:
regengrp /tmp/newgroup

I did not test it, but should work
sreeram n
Frequent Advisor

Re: Help required Unix script to compare group file.

Hi Laurent,

I have checked the below scripts and it is not giving any output. Could you please help me to resolve this. I am not scripting expert thats why I am troubling you.

Thanks
Sreeram

#!/usr/bin/ksh

while read in
do
n1="${in%%:*}"
n2="${in%:*}"
n2="${in#*:}"
users="${in##*:}"
while [ ! -z "$users" ]
do
echo "$n1" "$n2" "${users%%,*}"
newusers="${users#*,}"
if [ "$newusers" = "$users" ]
then
break
fi
done
done
.....
Sreeram N
Laurent Menase
Honored Contributor

Re: Help required Unix script to compare group file.

I think I found it:
while [ ! -z "$users" ]
do
echo "$n1" "$n2" "${users%%,*}"
newusers="${users#*,}"
if [ "$newusers" = "$users" ]
then
break
fi
users="$newusers"
done
done
Laurent Menase
Honored Contributor

Re: Help required Unix script to compare group file.

Found an other one.

#!/usr/bin/ksh

while read in
do
n1="${in%%:*}"
n2="${in%:*}"
n2="${n2#*::}"
users="${in##*:}"
while [ ! -z "$users" ]
do
echo "$n1" "$n2" "${users%%,*}"
newusers="${users#*,}"
if [ "$newusers" = "$users" ]
then
break
fi
users="$newusers"
done
done

Laurent Menase
Honored Contributor

Re: Help required Unix script to compare group file.

To regenerate
#!/usr/bin/ksh
N=""
while read a b c
do
if [ "$N" != "$a" ]
then
echo "\n$a::$b:$c\c"
$N="$a"
else
echo ",$c\c"
fi
done
echo "\n"
.....
Hein van den Heuvel
Honored Contributor

Re: Help required Unix script to compare group file.

Steven>> please send me a script that can do this job.

Please send me money.

:-)

Sounds good to me.
If we do a persons work, we get the money!

So 'points' nor pride cut it for you Steven ?
:-)

I agree with that, but I do have a de-luxe version floating around. In perl...


$ cat merge_groups.pl
use strict;
use warnings;

my ($old, $line, %groups,%x);
$old='';
while (<>) {
my $flagged;
chomp;
while (/(.*):(\d+):(\w+)/) {
my $g = sprintf("%05d", $2); # help sort
if ($groups{$g} && $1 ne $groups{$g}) {
print STDERR "Groupname mismatch on line: $_\n" unless $flagged ++;
} else {
$groups{"$g"} = $1;
}
$x{"$g $3"}++;
s/:$3,?/:/
}
}
foreach (sort keys %x) {
my ($g,$u) = split;
if ($g eq $old) {
$line .= ",$u";
} else {
print $line."\n" if $line;
$line = sprintf("%s:%d:%s",$groups{"$g"},$g,$u);
$old = $g;
}
}
print $line."\n";



usage:

#perl merge_groups.pl group1 group2 groupn > new


- sorts groups by number
- sorts users in the groups
- the first 'name' wins
- drops duplicate user names silently



sreeram n
Frequent Advisor

Re: Help required Unix script to compare group file.

Hi Hein van,

Thank you very much. Its works without any problem. One small isuue what I am facing is it drop the groups without out any member in it. If you can help me to resolve this it will be highly appreciable. I am not that much femiliar with scripts.

Thanks for Laurent for his kindness for sparing time to help me.

Regards
Sreeram
Sreeram N
Hein van den Heuvel
Honored Contributor
Solution

Re: Help required Unix script to compare group file.

Ok, you caught me. It wasn't really a /etc/group file merge I had handy, just something like it.
Here is is again, with support for empty lines like a default /etc/group file has.
Note... the sorting by username makes 'root' not (always) show up first.

Hein.

------------------------------
use strict;
use warnings;

my ($old, $key, %groups,%x);
$old='';
while (<>) {
my $flagged;
chomp;
if (/^(\w+:[^:]*):(\d+):/) { # Line starts with group name:x:number?
my $g = sprintf("%04d", $2);
if ($groups{$g} && $1 ne $groups{$g}) {
print STDERR "Groupname mismatch on line: $_\n" unless $flagged ++;
} else {
$groups{"$g"} = $1;
$x{$g}++ # Empty lines are ok
}
s/$`/:/; # Strip the name:group now that it is processed
while (/:(\w+)/) { # User on the line?
$x{"$g $1"}++; # Remember this user
s/:$1,?/:/ # Strip user once processed
} # users
} # group
} # line
foreach $key (sort keys %x) {
my ($g,$u) = split /\s+/,$key;
if ($g eq $old) {
$_ .= "$u,";
} else {
if ($_) {
s/,?$/\n/; # Terminate
print;
}
$u = "" unless $u; # empty is ok.
$_ = sprintf("%s:%d:%s",$groups{"$g"},$g,$u);
$old = $g;
}
}
s/,?$/\n/; # Terminate
print;
sreeram n
Frequent Advisor

Re: Help required Unix script to compare group file.

Hi Hein,

Thanks a lot man. This what I was looking.

It is really helping me to for user /group migration

Thanks again

Regards.

Sreeram

Sreeram N
sreeram n
Frequent Advisor

Re: Help required Unix script to compare group file.

Thanks a lot Hein's scripts solved my problem.

Thanks to all who helped me.
Sreeram N