The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK split slarionoff Shell Programming and Scripting 10 09-09-2009 08:36 AM
split -d vijay_0209 Shell Programming and Scripting 1 09-30-2008 03:52 AM
help with split Hawks444 Shell Programming and Scripting 1 02-28-2008 06:11 PM
Split a file with no pattern -- Split, Csplit, Awk madhunk UNIX for Dummies Questions & Answers 10 12-17-2007 12:57 PM
Split AkumaTay UNIX for Dummies Questions & Answers 1 08-18-2001 08:50 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-02-2009
ak835 ak835 is offline
Registered User
  
 

Join Date: Apr 2008
Location: Mumbai,India
Posts: 77
Thumbs up 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 (permalink)  
Old 10-02-2009
ghp ghp is offline
Registered User
  
 

Join Date: Dec 2008
Location: Zwevegem, Belgium
Posts: 13

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 (permalink)  
Old 10-02-2009
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,439
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 (permalink)  
Old 10-05-2009
ak835 ak835 is offline
Registered User
  
 

Join Date: Apr 2008
Location: Mumbai,India
Posts: 77
Thumbs up

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 (permalink)  
Old 10-05-2009
sagar_evc sagar_evc is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 30
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 (permalink)  
Old 10-06-2009
ak835 ak835 is offline
Registered User
  
 

Join Date: Apr 2008
Location: Mumbai,India
Posts: 77
Thumbs up

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 (permalink)  
Old 10-12-2009
ghp ghp is offline
Registered User
  
 

Join Date: Dec 2008
Location: Zwevegem, Belgium
Posts: 13
Drop the sed, you are losing important information.

Start with:

awk -F ":"
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0