Remove all duplicate word from string using shell script
One more awk, just for fun:
$ a="aaa bbb aaa bbb ccc aaa ddd bbb ccc"
$ echo "$a" | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s%s",$i,FS)}{printf("\n")}'
aaa bbb ccc ddd
By the way, even your solution works fine with variables:
$ b="zebra ant spider spider ant zebra ant"
$ echo "$b" | xargs -n1 | sort -u | xargs
ant spider zebra
With tr
, sort
and uniq
echo "zebra ant spider spider ant zebra ant" | tr ' ' '\n' | sort -u
or
echo "zebra ant spider spider ant zebra ant" | tr ' ' '\n' | sort -u | xargs
to get one line
$ echo "zebra ant spider spider ant zebra ant" | awk -v RS="[ \n]+" '!n[$0]++'
zebra
ant
spider