Please help!
I have a file having duplicate words in some line and I want to remove the duplicate words.
The order of the words in the output file doesn't matter.
INPUT_FILE
pink_kite red_pen ball pink_kite ball
yellow_flower white no white no
cloud nine_pen pink cloud pink nine_pen
brown_ball white
red_bear green red_bear
white no
OUTPUTFILE
pink_kite red_pen ball
yellow_flower white no
cloud nine_pen pink
brown_ball white
red_bear green
white no
Your help is highly appreciated.
Thanks in advance
#!/usr/bin/env python
for line in open('temp.txt', 'r'):
seen = []
words = line.rstrip('\n').split()
for word in words:
if not word in seen:
print word,
seen.append(word)
print
Output:
# cat temp.txt
pink_kite red_pen ball pink_kite ball
yellow_flower white no white no
cloud nine_pen pink cloud pink nine_pen
brown_ball white
red_bear green red_bear
white no
# python temp.py
pink_kite red_pen ball
yellow_flower white no
cloud nine_pen pink
brown_ball white
red_bear green
white no
I cannot reproduce the same error, obviously use nawk or /usr/xpg4/bin/awk on Solaris. The code works fine either on Solaris or Linux with no error messages.
HTH.