Help on capturing /etc/group info.....!!

Gurus

I am trying to capture all the data in /etc/group file in a CSV ,thru a fingerprinting engine.

For hosts having ,unique group names and Ids ,following code works fine.

Trouble starts when on a host,there are multiple groups defined with same name and id.

e.g One of my hosts has 8 groups defined with [same]name "operators" and [same] id "542".

Query1:
How can i capture all these 8 groups with following script lines?

Query2:

myHost        operators  542     jackS johnM 2009-12-01

I am getting two names in the third column (userlist).'jackS' is where first group entry ends and 'johnM' is where second or next entry begins.
How can this be avoided?

Query3:
Is it possible to define multiple groups with same name and id?


Family Name:Host etcGroup

DUP:
cat /etc/group | awk -F':' '{print $1$3}'

GroupName:
A=`cat /etc/group |awk -F':' '{if ($1$3 == "@@Host etcGroup.DUP@@" ) print $1}'|sort|uniq`
echo $A

GID:
A=`cat /etc/group |awk -F':' '{if ($1$3 == "@@Host etcGroup.DUP@@" ) print $3}'|sort|uniq`
echo $A


Userlist:
A=`cat /etc/group|awk -F':' '{if ($1$3 == "@@Host etcGroup.DUP@@" ) print $4}'|sort|uniq`

B=`echo $A|wc -w`

if [ $B = 0 ]
then
echo NULL
else
count=`echo $A| nawk -F, {'print NF'}`
i=1
while [ $i -le $count ]
do
str[$i]=`echo $A| cut -d, -f${i}`
echo "${str[$i]}"
i=`expr $i + 1`
done
fi

Regards
Abhi

Please post a bigger part of the input file (including the problematic records you mentioned) and an example of the expected output.

input here is : /etc/group file.

probably i got partial answer to first query by appending 'sort -u' at the end...but as you can see 'user6 user7' is still a problem....

I'll state an example here:

hostname  Groupname GID Userlist Date
myHost	operators	 542 542 542 542 542 542 542 542	user1	24/11/2009
myHost	operators	 542 542 542 542 542 542 542 542	user2	24/11/2009
myHost	operators	 542 542 542 542 542 542 542 542	user3	24/11/2009
myHost	operators	 542 542 542 542 542 542 542 542	user4	24/11/2009
myHost	operators	 542 542 542 542 542 542 542 542	user5	24/11/2009
myHost	operators	 542 542 542 542 542 542 542 542	user6 user7	24/11/2009

expected is:

myHost	operators	 542 user1	24/11/2009
myHost	operators	 542 user2	24/11/2009
myHost	operators	 542 user3	24/11/2009
myHost	operators	 542 user4	24/11/2009
myHost	operators	 542 user5	24/11/2009
myHost	operators	 542 user6 	24/11/2009
myHost	operators	 542 user7 	24/11/2009

Regards
Abhi

Thanks,
and the expected output should be ... ?

---------- Post updated at 03:46 PM ---------- Previous update was at 03:27 PM ----------

awk -F'\t' 'NR == 1 { print; next }
{ split($3, g, /  */)
  if (n = split($4, u, /  */)) 
    for (i=1; i<=n; i++) print $1, $2, g[2], u, $5
  else print $1, $2, g[2], $4, $5 }
  ' OFS='\t' infile

You may need to adjust the spaces/tabs for your needs.

Thanks for that !!

Unfortunately i can't use this as

1> i do not wish to change code lines much just to get a new row...

2> i already wrote to capture exactly what i wanted....i need to tweak something somewhere to avoid my issue....

3> your code is to be run directly from command prompt where i need to put it in an engine where i m guessing it might not work....

even i had written something like this bfor..

cat /etc/group | \
nawk '
{
   ABC  = split($0, field, ":");
   USERS = split(field[4],user, ",");
   if (USERS == 0) printf "%s\t%s\t%s\n", field[1], field[3], "NULL";
   for (i = 1; i <= USERS; i++)
       printf "%s\t%s\t%s\n", field[1], field[3], user;
}
'

but this can be run easily from shell ,one time....my engine keeps re-iterating over DUP value....

Thanks for the effort and suggestion !!

Regards
Abhi

If I understand correctly your code doesn't work and you don't want to use mine.
So post a sample of the original input file, your /etc/group file.
There are many helpful people here, they will help you with your task.

I'm sure that now you understand how important it is to post the complete requirement since the beginning :slight_smile:

Probably you misunderstood....my code works fine for most of the hosts...with some ,i am having issue.

thats why i had asked the 'third' query.....

Secondly i can't explain how the engine works....i explained it once on this forum and luckily i got the right feedback...today i don have time to do that again....

whether you understood my requirement or not...i really can't help as ,i said before,explaining how engine works is tiring....thanks for trying though !!

i needed help in tweaking my existing code...which is working fine for over 1000+ hosts...i can't change it drastically for few hosts....

Regards
Abhi

Trying to improve radoulov solution :wink:

# cat /etc/group
hostname  Groupname GID Userlist Date
myHot   operaors         54 542 542 542 542 542 542 542 user1 user9     24/11/2009
myHost  operators        542 542 542 542 542 542 542 542        user2   24/11/2009
myHost  operators        542 542 542 542 542 542 542 542        user3   24/11/2009
myHost  operators        542 542 542 542 542 542 542 542        user4   24/11/2009
myHost  operators        542 542 542 542 542 542 542 542        user5   24/11/2009
myHost  operators        542 542 542 542 542 542 542 542        user6 user7     24/11/2009

# awk -F'\t' 'NR>1{split($3, g, " ");n=split($4,u," ");for(i=0;++i<=n;){print $1,$2,g[1],u,$NF}next}1' OFS=\\t /etc/group
hostname  Groupname GID Userlist Date
myHot   operaors        54      user1   24/11/2009
myHot   operaors        54      user9   24/11/2009
myHost  operators       542     user2   24/11/2009
myHost  operators       542     user3   24/11/2009
myHost  operators       542     user4   24/11/2009
myHost  operators       542     user5   24/11/2009
myHost  operators       542     user6   24/11/2009
myHost  operators       542     user7   24/11/2009

OP whould watch for Useless Use of Cat Award :wink:

i'll reframe my problem...this should be easy to debug ...

bash-3.00$ A=`cat /etc/group|awk -F: '{if ($1$3 == "secur138"){print $NF}}'|sort|uniq`
bash-3.00$ echo $A
t9330sp,t0096pg,t6899bs,t2483eb,t1563dr,t6658pb t9330sp,t0096pg,t6899bs,t2483eb,t1563dr,t6658pb,smadmn,sysweb,root

See that green colored string....rest all strings(or members) are comma separated....whereas this string is not..it should have been t6658pb,t9330sp...

and its happening because

bash-3.00$  cat /etc/group|awk -F: '{if ($1$3 == "secur138"){print $NF}}'
t9330sp,t0096pg,t6899bs,t2483eb,t1563dr,t6658pb
t9330sp,t0096pg,t6899bs,t2483eb,t1563dr,t6658pb,smadmn,sysweb,root

i have two groups defined with same name and id ,but few diffrent members....

My code looks like following to capture these comma separated strings:

Host etcGroup DUP:

cat /etc/group | awk -F':' '{print $1$3}'|sort|uniq


Userlist:

A=`cat /etc/group|awk -F: '{if ($1$3 == "@@Host etcGroup.DUP@@") {print $NF}}'|sort|uniq`

B=`echo $A|wc -w`

if [ $B = 0 ]
then
echo NULL
else
count=`echo $A| nawk -F, {'print NF'}`
i=1
while [ $i -le $count ]
do
str[$i]=`echo $A| cut -d, -f${i}`
echo "${str[$i]}"
i=`expr $i + 1`
done
fi

any ideas?

Regards
Abhi

guys

any ideas/suggestions ?

Regards
Abhi

To anwer your query:

I use mutiple groups with the same GID but different names to get around the maximum string length problem, e.g.

group:x:1024:member1,member2,member3
groupa:x:1024:member4,member5,member6
groupb:x:1024:member7,member8,member9

If the have the same name then in Solaris only one of the lines would count, proof would be to run:

$ getent group <groupname>

To query the group that has two lines with the same name and GID using an OS call and see what gets listed.