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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

horizontal sorting 1

Status
Not open for further replies.

grazinggoat

Programmer
Mar 12, 2008
41
US
thread271-1560325

When I run the awk I get the following error:

cat /etc/group | awk -F':' '{split($4,a,","); asort(a);
printf "%s",$1":"$2":"$3;y=":";
for (i in a) {if(a!=x)printf"%s",y a;x=a;y=","}
> printf "%s",$1":"$2":"$3;y=":";
> for (i in a) {if(a!=x)printf"%s",y a;x=a;y=","}
> print ""}'
awk: illegal reference to array a
record number 1
 
Hello PHV

I also attempted to run yours but its only duplicating the lines and appending them. I would like to sort the fields
which is not being done

I ran your suggested awk:

awk '
BEGIN{FS=OFS=":"}
{split($4,a,",")
for(i in a)u[a]
for(i in u){x=x","i;delete u}
print $1,$2,$3,substr(x,2);x=""
}' /etc/group

thanks!
 
OK, try this instead:
Code:
awk '
BEGIN{FS=OFS=":"}
{n=split($4,a,",")
 for(i=1;i<n;++i)for(j=i+1;j<=n;++j)if(a[i]>a[j]){t=a[i];a[i]=a[j];a[j]=t}
 for(i=1;i<=n;++i)x=x","a[i]
 print $1,$2,$3,substr(x,2);x=""
}' /etc/group

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
works like a charm...
would you mind explaining the substr on the print... This is where I don't quite understand.

 
The substr get rid of the leading comma.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top