PDA

View Full Version : Distinct words in a line


anarhistu
October 27th, 2005, 05:04 PM
Using bash, preferably.

C in the worst case.

I need all the distinct words in a line separed by spaces.i did this with awk i think, a whilw ago, but I haven't used this kind of stuff for almost a year now....

LordHunter317
October 27th, 2005, 05:31 PM
Wait, do you just want to iterate every word on a line, or do you want to ignore words that have already been seen?

Pathogenix
October 27th, 2005, 05:43 PM
When I need a quick and dirty script, I whip out my python. I have this strange affection for a scripting language I can come back and read in a year's time and understand immediately.


#! /usr/bin/python2.4

from string import split
import sys
for line in sys.stdin:
unique = {}
for token in split(line):
unique[token] = 1
print "".join("%s " % k for k in unique.keys())


cat someFile | ./myPythonfile.py

anarhistu
October 28th, 2005, 04:13 AM
Cool idea, that is what I want to do.

lordHunter, I waNT TO ignore all words that hae already been seen.

Can this be done in bash?

In DOS I would make a program which would take the first parmeter (string) and create a string with all the other parameters, ignoring the ones equal to the first.Then I would recall the program with the new list, until the list of parameters is empty.

I know this can be done in bash, but i just don't know how.

I would prefer a one line awk script rather than a 10 lines sh program.I will study more, if you haven't got any ideas.

Pathogenix
October 28th, 2005, 06:01 AM
Actually, I googled the answer for Bash before writing the Python script.

http://cs.anu.edu.au/student/comp2100.2004/lab-exam-solution.html

You'd probably want to modify that solution a little to make it work line by line.

anarhistu
October 28th, 2005, 11:52 AM
Hehe, the nice clean quick way.
I figured it out before reading this solution:

cat whatever | tr ' ' '\n' | sort -u

And, if I want it in a line, i just tr[anslate] the newlines back to spaces.