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

Sort remove duplicates horizontally

Status
Not open for further replies.

grazinggoat

Programmer
Mar 12, 2008
41
US
Hello - How can I sort uniq (deletes duplicates) of the second field then writes it back to the file:

users1 PROD = bill,tom,larry,john
users2 PROD = bill,tom,bill,larry,john
users3 PROD = bill,tom,larry,john,bill

so that it produces

users1 PROD = bill,john,larry,tom
users2 PROD = bill,john,larry,tom
users3 PROD = bill,john,larry,tom

This is what I get:

cat test.txt|awk -F'=' '{split($2,a,",")asort(a);
> for(i in a)u[a]
> for(i in u){x=x","i;delete u}
> print $2
> {'
awk: syntax error near line 5
awk: illegal statement near line 5
 
Hi

Code:
[red][s][black]cat test.txt|[/black][/s][/red]awk -F'=[highlight] *[/highlight]' '{split($2,a,",")[highlight];[/highlight]asort(a); [gray]# UUOC; trim leading spaces; missing semicolon[/gray]
for(i in a)u[a[i]]
[highlight]x=""[/highlight] [gray]# reset before use[/gray]
for(i in u)[red][s][black]{[/black][/s][/red]x=x","i[red][s][black];delete u[i]}[/black][/s][/red] [gray]# no need to delete[/gray]
print [red][s][black]$2[/black][/s][/red][highlight]x[/highlight] [gray]# print the result, not the original[/gray]
[red][s][black]{[/black][/s][/red][highlight]}[/highlight]' [highlight]text.txt[/highlight] [gray]# close the brace, not open it; here you read the file[/gray]

Feherke.
 
FYI the answer was already here:
thread271-1560325

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

Part and Inventory Search

Sponsor

Back
Top