![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| to find numbers in a string | fongthai | Shell Programming and Scripting | 12 | 11-22-2007 04:34 PM |
| How do i get numbers from a string? | eliraza6 | Shell Programming and Scripting | 13 | 07-18-2007 07:04 AM |
| perl: string comparison as numbers | bishweshwar | UNIX for Advanced & Expert Users | 7 | 05-27-2007 12:54 AM |
| Sort Numbers in ascending orders | Raynon | Shell Programming and Scripting | 7 | 01-19-2007 02:03 AM |
| output only numbers from mixed string | nortypig | Shell Programming and Scripting | 10 | 08-29-2006 10:30 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
How to sort a string with numbers
Hi,
I need help to sort a file contents. I am using sort -r option to basically reverse the comparison in descending order. However, i found out that my file is not sorted according, can anyone please help. My data is something like:- Hello world 20.982342864 343 19.234355545 222 1.5567846 345 17.8767787 200 If I use the option -g or -n it works..but it is in ascending order. but I would like to display in descending order.I tried -nr but the Hello world line would be reverse as well as the last item. My desired output would be Hello world 20.982342864 343 19.234355545 222 17.8767787 200 1.5567846 345 Thanks. Last edited by ahjiefreak; 12-21-2007 at 12:48 AM.. |
|
||||
|
Quote:
Code:
sort -r -k 1,1 file |
|
||||
|
What you need to do is tell "sort" that you want to sort numerically instead of alphabetically: "1,2,13,20" is sorted numerically, alphabetically sorted it would look like "1,13,2,20", because "1" comes before "2" in the "alphabet" (the ASCII code) the sort command uses. Aphabetical sorting is the default, numerical sorting can bei achieved by using the option "-n" as you have already found out. If you want to reverse the sorting you have to use "-r" as you have already noticed too. So combine these two to get:
Code:
"cat file | sort -rn" bakunin |
|
||||
|
Code:
# sort -rn file 20.982342864 343 19.234355545 222 17.8767787 200 1.5567846 345 Hello world |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|