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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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?
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.
line 1: syntax error near unexpected token '('
. Appreciate if you could kindly suggest how to avoid the error and execute as a script file. Thanks a lot.
sh
does not support process substitution e.g.: <(command)
. You can try with bash
.
(I like the paste/cut answer of @taliezin. Here's a more prosaic solution in case of many columns).
Split the data into one file per column, sort these files, and merge back the
files. I'm assuming fixed-width columns of 1 character, to cope with
column 2 being shorter than column 3, for example.
In this demo data0
is the initial file:
for i in {1..4}
do awk -v i=$i '{ch = substr($0,i*2-1,1);if(ch!=" ")print ch}' </tmp/data0 |
sort -u >/tmp/data$i
done
awk -v rows=$(wc -l </tmp/data0) '
BEGIN{
for(i=1;i<=4;i++)
file[i] = "/tmp/data" i
while(rows-->0){
for(i=1;i<=4;i++){
d = ""
getline d <file[i]
printf("%1s ",d)
}
printf "\n"
}
}' </dev/null
ulimit -n
.