Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix sort descending order

Tags:

unix

sorting

I want to sort a tab limited file in descending order according to the 5th field of the records.

I tried

sort -r -k5n filename 

But it didn't work.

like image 707
user1598776 Avatar asked Aug 14 '12 17:08

user1598776


People also ask

How do you sort data in descending order in Unix?

-r Option: Sorting In Reverse Order: You can perform a reverse-order sort using the -r flag. the -r flag is an option of the sort command which sorts the input file in reverse order i.e. descending order by default. Example: The input file is the same as mentioned above.

How do I sort descending order in Linux?

Sort a File Numerically To sort a file containing numeric data, use the -n flag with the command. By default, sort will arrange the data in ascending order. If you want to sort in descending order, reverse the arrangement using the -r option along with the -n flag in the command.

Which command is used for descending order?

The ORDER BY command is used to sort the result set in ascending or descending order. The ORDER BY command sorts the result set in ascending order by default. To sort the records in descending order, use the DESC keyword.

How do you sort a file in Unix?

In Unix, the sort command with the ‘r’ option gives you to sort the contents in reverse order. This option sorts the given input in a reverse way which is by default in descending order. 3. Option -n In Unix, when you try to sort a file in a numeric way, you can use the option ‘-n’ with the sort command.

How to sort text in Unix in reverse order?

An example is shown below: 2. Option -r In Unix, sort command with ‘r’ option gives you to sort the contents in reverse order. This option sorts the given input in a reverse way which is by default in descending order.

What is the use of sort-R in Linux?

sort -r: It reverses the contents of files and sorts the order. sort -o: It specifies and prints the output file in a sorted way. sort -n: It is used to sort the numerical value in ascending order. sort -M: It helps to sort the contents as per the calendar month.

What are the options used by sort command?

Below are some of the list of options that are generally used by sort: sort -b: It ignores the blank spaces at the start of the data in the file. sort -r: It reverses the contents of files and sorts the order. sort -o: It specifies and prints the output file in a sorted way. sort -n: It is used to sort the numerical value in ascending order.


2 Answers

The presence of the n option attached to the -k5 causes the global -r option to be ignored for that field. You have to specify both n and r at the same level (globally or locally).

sort -t $'\t' -k5,5rn 

or

sort -rn -t $'\t' -k5,5 
like image 189
Alan Curry Avatar answered Oct 18 '22 15:10

Alan Curry


If you only want to sort only on the 5th field then use -k5,5.

Also, use the -t command line switch to specify the delimiter to tab. Try this:

sort  -k5,5 -r -n -t \t filename 

or if the above doesn't work (with the tab) this:

sort  -k5,5 -r -n -t $'\t' filename 

The man page for sort states:

-t, --field-separator=SEP use SEP instead of non-blank to blank transition

Finally, this SO question Unix Sort with Tab Delimiter might be helpful.

like image 29
Levon Avatar answered Oct 18 '22 16:10

Levon