The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-21-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
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..
  #2 (permalink)  
Old 12-21-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try...
Code:
awk 'NR==1; NR>1{print|"sort -rn"}' file1
  #3 (permalink)  
Old 12-21-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
Code:
# more file
Hello world
20.982342864 343
19.234355545 222
1.5567846 345
17.8767787 200

# sort -r file
Hello world
20.982342864 343
19.234355545 222
17.8767787 200
1.5567846 345

# echo $LC_CTYPE
en_US.UTF-8
  #4 (permalink)  
Old 12-21-2007
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by ahjiefreak View Post
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.
Code:
sort -r -k 1,1 file
  #5 (permalink)  
Old 12-21-2007
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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"
to get the desired result.

bakunin
  #6 (permalink)  
Old 12-21-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
Quote:
Originally Posted by bakunin View Post
Code:
"cat file | sort -rn"
Code:
# sort -rn file
20.982342864 343
19.234355545 222
17.8767787 200
1.5567846 345
Hello world
my output puts the "hello world" at the bottom, which is not what OP wants. is your output the same?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0