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
Using perl
:
perl -lane '
%a = map { $_ => 1 } @F;
print join qq[ ], sort keys %a;
' <<< "zebra ant spider spider ant zebra ant"
Result:
ant spider zebra
Use tr
to change spaces to new lines, then sort
, and finally change new lines back to spaces.
echo $(tr ' ' '\n' <<< "zebra ant spider spider ant zebra ant" | sort -u)
The shell was built to parse [:blank:]
seperated word lists already. Therefore the use of xargs is completely redundant. The "unique" stuff can be done but its just easier to use sort.
echo $(printf '%s\n' zebra ant spider spider ant zebra ant | sort -u)
python
$ echo "zebra ant spider spider ant zebra ant" | python -c 'import sys; print(" ".join(sorted(set(sys.stdin.read().split()))))'
ant spider zebra
awk
:awk '{for(i=1;i<=NF;i++) a[$i]++} END{for(i in a) printf i" ";print ""}' INPUT_FILE
[jaypal:~/Temp] cat file
zebra ant spider spider ant zebra ant
[jaypal:~/Temp] awk '{for (i=1;i<=NF;i++) a[$i]++} END{for (i in a) printf i" ";print ""}' file
zebra spider ant
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
.