Computing.Net > Forums > Unix > delete repeated words in a line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

delete repeated words in a line

Reply to Message Icon

Name: hunter
Date: December 18, 2003 at 09:05:15 Pacific
OS: hp - unix
CPU/Ram: p4 & 256 mb
Comment:

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,b

output should be
a,b,c
a,b
thnx in advance



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: December 18, 2003 at 16:06:57 Pacific
Reply:

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 less

print $0 # print the output line

# reinitialize the array
for (c in arrs)
delete arrs[c]

} ' data.file



0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: delete repeated words in a line

Search for words in a text file www.computing.net/answers/unix/search-for-words-in-a-text-file/7915.html

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html

Find duplicate words in a file www.computing.net/answers/unix/find-duplicate-words-in-a-file/7999.html