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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split these into many ...(/etc/group)!!
# 1  
Old 10-02-2009
MySQL 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).


Code:
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

Last edited by ak835; 10-02-2009 at 08:00 AM..
# 2  
Old 10-02-2009
Code:
awk '
 {
   split($0,A,":");
   split(A[4],UL,",");
   for (U in UL) {print A[1] "\t" A[2] "\t" A[3] "\t" UL[u]}
 }' /etc/group


You probably meant "achieve" iso "achive". Help is not always appreciated. ;-)
# 3  
Old 10-02-2009
This will split the last line the way you want
Code:
awk -F'[:|,]' 'END{for(i=3;++i<=NF;)print $1,$2,$3,$i}' OFS="<TAB>" file

# 4  
Old 10-05-2009
MySQL

Thanks for the replies guys.

Code:
<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[u]}
>  }' c


lp      !       11
lp      !       11
lp      !       11

this is not giving me what i want.

Regards
Abhi
# 5  
Old 10-05-2009
Quote:
1. awk '
2. {
3. split($0,A,":");
4. split(A[4],UL,",");
5. for (U in UL) {print A[1] "\t" A[2] "\t" A[3] "\t" UL[u]}
6. }' /etc/group
Change in the line 5 ->
Code:
for (U in UL) {print A[1] "\t" A[2] "\t" A[3] "\t" UL[u]}

U should be in upper case in UL[U]

Last edited by sagar_evc; 10-05-2009 at 11:38 AM.. Reason: u in square brackets is always shown as lower case u
# 6  
Old 10-06-2009
MySQL

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

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

Code:
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];
}
'

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

Code:
lp:!:11:root,lp,printq

into

Code:
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...

Code:
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

Code:
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

Code:
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?

Code:
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 ----------

Code:
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
# 7  
Old 10-12-2009
Drop the sed, you are losing important information.

Start with:

awk -F ":"
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Process split files with same name by group

File examples f17_mar_01_02_03_04_fsw1.xml f17_mar_01_02_03_04_fsw2.xml f17_mar_01_02_03_04_fsw3.xml f17_feb_13_20_49_06_fsw1.xml f17_feb_13_20_49_06_fsw2.xml f17_feb_13_20_49_06_fsw3.xml I have many xml files that are grouped with same file name, but are numbered from 1-to-many... (2 Replies)
Discussion started by: aachave1
2 Replies

2. Shell Programming and Scripting

ksh : split hex number group

Hi, sry for poor english I have a group of hex number as : 4D40:4D42 I want so split this group in a list as : 4D40,4D41,4D42 i don't know how i can do this in ksh Thanks (5 Replies)
Discussion started by: jocazh
5 Replies

3. Programming

Sql ORA-00937: not a single-group group function

I'm trying to return only one row with the highest value for PCT_MAX_USED. Any suggestions? When I add this code, I get the ORA-00937 error. trunc(max(decode( kbytes_max, 0, 0, (kbytes_alloc/kbytes_max)*100))) pct_max_used This is the original and returns all rows. select (select... (3 Replies)
Discussion started by: progkcp
3 Replies

4. UNIX for Dummies Questions & Answers

Split binary file every occurrence of a group of characters

Hello I am new to scripts, codes, bash, terminal, etc. I apologize this my be very scattered because I frankly don't have any idea where to begin and I have had trouble sleeping lately. I have several 2GB files I wish to split. This Code 00 00 01 BA ** ** ** ** ** ** ** ** C3 F8 00 00 01 BB 00... (17 Replies)
Discussion started by: PatrickE
17 Replies

5. Shell Programming and Scripting

need a one liner to grep a group info from /etc/group and use that result to search passwd file

/etc/group tiadm::345:mk789,po312,jo343,ju454,ko453,yx879,iy345,hn453 bin::2:root,daemon sys::3:root,bin,adm adm::4:root,daemon uucp::5:root /etc/passwd mk789:x:234:1::/export/home/dummy:/bin/sh po312:x:234:1::/export/home/dummy:/bin/sh ju454:x:234:1::/export/home/dummy:/bin/sh... (6 Replies)
Discussion started by: chidori
6 Replies

6. Shell Programming and Scripting

Split, Search and Reformat by Data Group

Hi, I am writing just to share my appreciation for help I have received from this site in the past. In a previous post Split File by Data Group I received a lot of help with a troublesome awk script to reformat some complicated data blocks. What I learned really came in hand recently when I... (1 Reply)
Discussion started by: mkastin
1 Replies

7. Shell Programming and Scripting

Sort the file contents in each group....print the group title as well

I've this file and need to sort the data in each group File would look like this ... cat file1.txt Reason : ABC 12345-0023 32123-5400 32442-5333 Reason : DEF 42523-3453 23345-3311 Reason : HIJ 454553-0001 I would like to sort each group on the last 4 fileds and print them... (11 Replies)
Discussion started by: prash184u
11 Replies

8. Shell Programming and Scripting

split file based on group count

Hi, can some one please help me to split the file based on groups. like in the below scenario x indicates the begining of the group and the file should be split each with 2 groups below there are 10 groups it should create 5 files. could you please help? (4 Replies)
Discussion started by: hitmansilentass
4 Replies

9. Shell Programming and Scripting

Split file by data group

Hi all, I'm having a little trouble solving a file split I need to get done. I have the following data: 1. Light 1A. Light Soft texture: it's soft color: the color value is that of something light vital statistics: srm: 23 og: 1.035 sp: 1.065 comment: this is nice if you like... (8 Replies)
Discussion started by: mkastin
8 Replies
Login or Register to Ask a Question