Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use awk to do horizontal sort

Status
Not open for further replies.

grazinggoat

Programmer
Joined
Mar 12, 2008
Messages
41
Location
US
Hello

I'm attempting to do a horizontal sort of /etc/group file to remove duplicate ids on same line

example:

billing::22:davew,billm,carolb,annas,davew,jackt

How can I sort uniq this with awk?
I looked at sort, uniq, cut, and tr commands but couldn't get the uid fields sorted unique.

Thanks for any insight here
 
Something like this ?
Code:
awk 'BEGIN{FS=OFS=":"}{split($4,a,",");for(i in a)u[a[i]];for(i in u)x=x","i;print $1,$2,$3,substr(x,2)}' /etc/group

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OOps, need some cleaning ...
Code:
awk '
BEGIN{FS=OFS=":"}
{split($4,a,",")
 for(i in a)u[a[i]]
 for(i in u){x=x","i;delete u[i]}
 print $1,$2,$3,substr(x,2);x=""
}' /etc/group

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Other alternative:
Code:
echo "billing::22:davew,billm,carolb,annas,davew,jackt"|\
awk -F':' '{split($4,a,","); asort(a);
printf "%s",$1":"$2":"$3;y=":";
for (i in a) {if(a[i]!=x)printf"%s",y a[i];x=a[i];y=","}
print ""}'

$ ./t1
billing::22:davew,jackt,annas,billm,carolb
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
FYI, asort() is a GNU extension.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top