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.
-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.
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.
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.
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With