Sort Numbers in ascending orders | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Sort Numbers in ascending orders

Shell Programming and Scripting


Tags
awk, awk trim, trim, trim awk

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-17-2007
Raynon Raynon is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 18 April 2011, 3:56 AM EDT
Location: Sg
Posts: 353
Thanks: 0
Thanked 2 Times in 2 Posts
Sort Numbers in ascending orders

Hi All,

I am new to Shell programming. I have problems creating a csh script which can sort numbers in ascending orders using array.
For eg, given the following numbers: 8, 56, 15, 37, 21.
I would want the output to be in the following: 8,15,21,37,56.

Can anybody help me with this ?
Sponsored Links
    #2  
Old 01-17-2007
kapilraj kapilraj is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 20 December 2010, 5:45 PM EST
Location: Maryland
Posts: 163
Thanks: 0
Thanked 0 Times in 0 Posts
cat filename | sort -kn1

I hv no idea to use arrays.
Sponsored Links
    #3  
Old 01-17-2007
Raynon Raynon is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 18 April 2011, 3:56 AM EDT
Location: Sg
Posts: 353
Thanks: 0
Thanked 2 Times in 2 Posts
Hi Kap,

What does the -kn1 stand for ?
    #4  
Old 01-17-2007
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
$ echo "8,56,15,37,21" | tr ',' '\n' | sort -k1,1n | paste -s -d',' -
8,15,21,37,56

Cheers
ZB
Sponsored Links
    #5  
Old 01-18-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts
If you have Python:

Code:
echo "8,56,15,37,21" | python -c "print sorted(map(int,raw_input().split(',')))"

output:

Code:
[8, 15, 21, 37, 56]

Sponsored Links
    #6  
Old 01-18-2007
Raynon Raynon is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 18 April 2011, 3:56 AM EDT
Location: Sg
Posts: 353
Thanks: 0
Thanked 2 Times in 2 Posts
Hi ,

Can anybody tell me what does the syntax "tr" , "k1" , "1n" mean in the below code ?

echo "8,56,15,37,21" | tr ',' '\n' | sort -k1,1n | paste -s -d',' -
Sponsored Links
    #7  
Old 01-18-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 28 July 2014, 9:39 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
Do you have access to man pages? Can't you look these up there? Come on, the point of these forums is not to explain each and every command, is it?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ascending and descending sort Fafa Shell Programming and Scripting 3 06-25-2010 12:03 PM
Arbitrary sort orders treesloth UNIX for Dummies Questions & Answers 2 06-17-2010 11:13 AM
Sort ascending and descending don_0110 UNIX for Dummies Questions & Answers 1 11-27-2008 02:59 PM
Ascending & Descending order numbers pravani1 Shell Programming and Scripting 6 06-03-2008 07:43 AM
Sort / ascending order gyik UNIX for Dummies Questions & Answers 1 03-05-2001 09:08 AM



All times are GMT -4. The time now is 03:48 PM.