Split these into many ...(/etc/group)!!

Guys

Following input line is from /etc/group file.As we know last entry in a line of /etc/group is userlist (all the users belonging to that group).

I need to splilt this one line into 3 lines as shown below (3 because userlist has 3 names in it).

Input:
lp:!:11:root,lp,printq

Output:
lp<TAB>!<TAB>11<TAB>root
lp<TAB>!<TAB>11<TAB>lp
lp<TAB>!<TAB>11<TAB>printq

This way i need to do for all the entries in /etc/group.

I need code lines to achive it.

Help Appreciated !!

Regards
Abhi

awk '
 {
   split($0,A,":");
   split(A[4],UL,",");
   for (U in UL) {print A[1] "\t" A[2] "\t" A[3] "\t" UL}
 }' /etc/group

You probably meant "achieve" iso "achive". Help is not always appreciated. :wink:

This will split the last line the way you want

awk -F'[:|,]' 'END{for(i=3;++i<=NF;)print $1,$2,$3,$i}' OFS="<TAB>" file

Thanks for the replies guys.

<Myhost>:$ C='lp:!:11:root,lp,printq'
<Myhost>:$ echo $C
lp:!:11:root,lp,printq
<Myhost>:$ echo $C > c
<Myhost>:$ cat c
lp:!:11:root,lp,printq
<Myhost>:$ awk '
>  {
>    split($0,A,":");
>    split(A[4],UL,",");
>    for (U in UL) {print A[1] "\t" A[2] "\t" A[3] "\t" UL}
>  }' c


lp      !       11
lp      !       11
lp      !       11

this is not giving me what i want.

Regards
Abhi

Change in the line 5 ->

for (U in UL) {print A[1] "\t" A[2] "\t" A[3] "\t" UL}

U should be in upper case in UL[u]

yup...i shud have seen that...thanks...!!

even i tried writing something ,though its not giving me entirely what i want..

cat /etc/group | nawk '
{
   TUP  = 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;
}
'

I m just setting NULL wherever userlist if NULL.

Can someone tell me how do i split the row by removing ':' from it into just a row with n columns

lp:!:11:root,lp,printq

into

lp ! 11 root,lp,printq

such that i can just pick column values using 'awk'.

Regards
Abhi

---------- Post updated at 02:25 PM ---------- Previous update was at 02:02 PM ----------

okay...this might be it...

bash-3.00$ D='lp:!:11:root,lp,printq'
bash-3.00$ echo $D |sed 's/:/ /g'
lp ! 11 root,lp,printq
bash-3.00$ echo $D |sed 's/:/ /g'|awk '{print $1}'
lp
bash-3.00$ echo $D |sed 's/:/ /g'|awk '{print $2}'
!
bash-3.00$ echo $D |sed 's/:/ /g'|awk '{print $3}'
11
bash-3.00$ echo $D |sed 's/:/ /g'|awk '{print $4}'
root,lp,printq

Regards
Abhi

---------- Post updated at 02:39 PM ---------- Previous update was at 02:25 PM ----------

now this one is from /etc/passwd file

abhi:!:34971:3418:abhi k, Sys Admin Level 3:/home/users/abhi:/bin/ksh

As we can see,in comments section i have put something against my user id.

using the same code above pasted

bash-3.00$ echo $D|sed 's/:/ /g'|awk '{print $5 $6 $7 $8 $9 $10}'

abhik,SysAdminLevel3

I want "abhi k ,Sys Admin Level 3" to be put in just one column as it is (no space deletion).Rest columns are easy to get.

Any ideas ?

Regards
Abhi

---------- Post updated at 03:48 PM ---------- Previous update was at 02:39 PM ----------

i thought this wud work...but it isn't....anything missing?

bash-3.00$ echo $D|sed 's/:/ /g'|awk -F' ' '{$1=$2=$3=$4="";sub("^    ","");print}'
abhi k, Sys Admin Level 3 /home/users/abhi/ /bin/ksh

i just need "abhi k, Sys Admin Level 3".....

Regards
Abhi

---------- Post updated at 04:12 PM ---------- Previous update was at 03:48 PM ----------

bash-3.00$ echo $D|sed 's/:/ /g'|awk -F' ' '{$1=$2=$3=$4="";sub("^    ","");print}'|cut -d "/" -f1
abhi k, Sys Admin Level 3

i hope i am not sounding crazy here....

Regards
Abhi

Drop the sed, you are losing important information.

Start with:

awk -F ":"

Thanks !! but i did not understand what important info....

even with awk -F':' ' , i m getting same result...

bash-3.00$ D='abhi:!:34971:3418:abhi k, Sys Admin Level 3:/home/users/abhi:/bin/ksh'
bash-3.00$ echo $D|awk -F':' '{$1=$2=$3=$4="";sub("^    ","");print}'
abhi k, Sys Admin Level 3 /home/users/abhi /bin/ksh
bash-3.00$ echo $D|awk -F':' '{$1=$2=$3=$4="";sub("^    ","");print}'|cut -d "/" -f1
abhi k, Sys Admin Level 3

would appreciate if you can point-out !!

Regards
Abhi

awk -F ':' '{print $5}' /etc/passwd

okay...

now i badly need advice...

I have a fingerprinting tool thru which i generate a lot of data in CSVs.

I need all of information in /etc/group file.

lets say,i want to get following line in my CSV.My requirement here is bit tricky.I want 2 separate entries in CSV for following line.

bin:!:2:root,bin

should look like

bin 2 root
bin 2 bin

now this is how i have written command lines in my fingerprinting tool:

ParentFamily : Host etcGroup

GroupName:

cat /etc/group |awk '{print $1}' | cut -d ":" -f1

GID

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

UserList

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

B=`echo $A|wc -w`

C=`echo $A|grep "\,"|wc -l`

D=`echo $A|awk -F',' '{ print NF }'`

if [ $B = 0 ]
then
echo NULL
fi

if [ $B = 1 ]
then
   if [ $C = 0 ]
   then
   echo $A
   else
   XXXXXXX
   XXXXXXX
fi
fi

As you can see,i am stuck at 'XXXXXX' place.I am not sure what to write there to get what is required .

Regards
Abhi

# echo 'bin:!:2:root,bin' | awk -F'[:|,]' 'END{for(i=3;++i<=NF;)print $1,$3,$i}'
bin 2 root
bin 2 bin

You are not stuck, just make your life complicated.

well...thanks for this !!

echo 'bin:!:2:root,bin' | awk -F'[:|,]' 'END{for(i=3;++i<=NF;)print $1,$3,$i}'
bin 2 root
bin 2 bin

but the fingerprinting engine does not work this way.I have defined 3 columns totally ,'GroupName' being first and am fetching 'GroupName' in next 2columns.

whatever data i want to see in CSV,tool has to generate it...its a complicated thing ....can't help it !!

to get something like 'bin:!:2:root,bin' before passing to 'awk',i need to write additonal lines and even after it may not work.

I'll try this and would post my update.

Regards
Abhi

This problem has been dealt with in a different thread, recently.

Can you point me to that thread pls?

By the way,i found solution to my issue...i divided my code into parent-child relationship and wrote following lines for child fingerprint....the 'while' loop is not mine..i took it from very this forum....

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

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

Thanks to everyone who responded !!

Regards
Abhi