Issue
The sort
command lets me put lines in alphabetical order and remove duplicate lines. I need something similar that can sort the words on a single line, put them in order, and remove any duplicates. Is there a command for this?
E.g.:
zebra ant spider spider ant zebra ant
Changes to:
ant spider zebra
There is no space before the first word or after the last word.
Solution
This works for me:
$ echo "zebra ant spider spider ant zebra ant" | xargs -n1 | sort -u | xargs
ant spider zebra
You can transform a list of words in a single row to a single column with xargs -n1
, use sort -u
and transform back to a single row with xargs
.
Answered By - jcollado Answer Checked By - Katrina (WPSolving Volunteer)