Sort and uniq columns individually in a text file

sortuniq

I have a file as below:

D F T E
A R T E
K A O E
E T P J

I would like to sort each column and unique the columns individually as below:

A A O E
D F P J
E R T
K T

I wonder if there is anyone who knows how to use sort or uniq to do so?

Best Answer

You can try something like this:

paste -d'\t' <(cut -f 1 -d' ' file | sort -u) <(cut -f 2 -d' ' file | sort -u) <(cut -f 3 -d' ' file | sort -u) <(cut -f 4 -d' ' file | sort -u) >output

I put tab as delimiter of paste to be more visible the output.

Related Question