Linux awk descending sort does not work … here is a solution to the problem.
Linux awk descending sort does not work
I have two files to sort.
The command I used was:
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 questions are:
I need to sort correctly in descending order because now only file 2 is sorted correctly, but file 1 is not sorted correctly.
I
wonder what mistake I made
file
All files are here including output are here
Thanks in advance.
Solution
I guess you mean it’s wrong :
4.2.4
4.2.3
4.2.20
4.2.2
You want 4.2.20
to be higher than all of that, right?
You can work around this by changing the -k
parameter of sort
to treat all fields as numbers:
.... -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr -k 5,5nr ....