##Python
Python
###Option 1
Option 1
#!/usr/bin/env python
# get_unique_words.py
import sys
l = []
for w in sys.argv[1].split(','):
if w not in l:
l += [ w ]
print ','.join(l)
Make executable, then call from Bash:
$ ./get_unique_words.py "aaa,aaa,aaa,bbb,bbb,ccc,bbb,ccc"
aaa,bbb,ccc
Or you could implement it as a Bash function, but the syntax is messy.
get_unique_words(){
python -c "
l = []
for w in '$1'.split(','):
if w not in l:
l += [ w ]
print ','.join(l)"
}
Option 2
This option can become a one-liner if needed:
#!/usr/bin/env python
# get_unique_words.py
import sys
s_in = sys.argv[1]
l_in = s_in.split(',') # Turn string into a list.
set_out = set(l_in) # Turning a list into a set removes duplicates items.
s_out = ','.join(set_out)
print s_out
In Bash:
get_unique_words(){
python -c "print ','.join(set('$1'.split(',')))"
}