miloski miloski - 1 month ago 6
Bash Question

How do i ordanate a column without ordanating a line?

can someone explain me how do i do this?
I have a file that has three columns, and a lot of lines, and i want that the second column to be sorted in ascending order (it contains only numbers).

sample file;

7.31937 736 /tmp/ref13
7.3223 5373 /tmp/ref13
7.32816 768 /tmp/ref13
7.32955 5370 /tmp/ref10


I want to;

7.31937 736 /tmp/ref13
7.3223 768 /tmp/ref13
7.32816 5370 /tmp/ref13
7.32955 5373 /tmp/ref10


Thanks!!

Answer

you can try this ;

paste -d' '  <(awk '{print $1}' yourFile) <(awk '{print $2}' yourFile | sort -n) <(awk '{print $3}' yourFile)

or

awk '{print $2}' yourFile | sort -n | paste yourFile - | awk '{print $1"\t"$4"\t"$3}'

Eg:

user@host:/tmp$ cat t1
7.31937  736    /tmp/ref13
7.3223   5373   /tmp/ref13
7.32816  768    /tmp/ref13
7.32955  5370   /tmp/ref10

user@host:/tmp$ paste -d' '  <(awk '{print $1}' t1) <(awk '{print $2}' t1 | sort -n) <(awk '{print $3}' t1)  | column -t
7.31937  736   /tmp/ref13
7.3223   768   /tmp/ref13
7.32816  5370  /tmp/ref13
7.32955  5373  /tmp/ref10