user3155632
user3155632

Reputation: 484

Linux awk sort descending order not working

I have two files that I need to sort with.

The command I'm using is:

cat first-in.txt | awk '{print $2}' | cut -d '/' -f 3 | cut -d '^' -f 1 | sort -b -t . -k 1,1nr -k 2,2nr -k 3,3r -k 4,4r -k 5,5r | uniq > first-out.txt


cat second-in.txt | awk '{print $2}' | cut -d '/' -f 3 | cut -d '^' -f 1 | sort -b -t . -k 1,1nr -k 2,2nr -k 3,3r -k 4,4r -k 5,5r | uniq > second-out.txt

the issue is: I need to sort CORRECTLY in descending order, because right now, only file 2 is sorting correctly, but file 1 is not sorting correctly.

i would like to know the mistake i am making

Files

All files are here including output are here

Thanks in advance.

Upvotes: 3

Views: 2226

Answers (2)

Dimitre Radoulov
Dimitre Radoulov

Reputation: 28000

On GNU/Linux system you could use sort with the -V option:

sed -r 's|.*/([^/^]*).*$|\1|' infile | sort -Vr

Note that both sed -r and sort -V are not standard.

Upvotes: 1

janos
janos

Reputation: 124646

I guess you mean this is wrong:

4.2.4
4.2.3
4.2.20
4.2.2

You want 4.2.20 to be higher than all of those, right?

You can fix that by change the -k param of sort to treat all fields as numeric:

.... -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr -k 5,5nr ....

Upvotes: 3

Related Questions