Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
how can i delete repeated words in a line
ex. i have a file in the order
a,b,a,b,c
a,b,a,boutput should be
a,b,c
a,b
thnx in advance

Hunter:
About the only way to do this, is to parse each line and save the elements in an array. I used the associative properties of awk arrays to solve this problem. I'm using nawk on Solaris 7:
nawk ' BEGIN { FS=","; OFS="," }
{for (i=1; i<=NF; i++)
if($i in arrs)
x=1 # dummy statement
else
arrs[$i]=1 # index is the field value# build output line from the array
f=1
for (b in arrs)
{
$f=b
f++
}
NF=f-1 # number of fields is one lessprint $0 # print the output line
# reinitialize the array
for (c in arrs)
delete arrs[c]} ' data.file

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |