Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-17-2007
Registered User
 
Join Date: Sep 2006
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
Registered User
 
Join Date: Dec 2006
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
Registered User
 
Join Date: Sep 2006
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
Registered Geek
 
Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,186
Thanks: 3
Thanked 23 Times in 22 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
Registered User
 
Join Date: Sep 2006
Posts: 2,651
Thanks: 0
Thanked 15 Times in 15 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
Registered User
 
Join Date: Sep 2006
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
AFK
 
Join Date: Dec 2004
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

Tags
awk, awk trim, trim, trim awk

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 06:26 AM.