LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-18-2012, 03:21 PM   #1
robfraser
LQ Newbie
 
Registered: Apr 2012
Posts: 1

Rep: Reputation: Disabled
Sort numbers in ascending order using echo, and as an alias


I am trying to sort a list of numbers in ascending order. I got the following from another closed thread. (Thank You)

echo "8,56,15,37,21" | tr ',' '\n'|sort -n|paste -s -d','

I would now like to use it as an alias, so I can enter a list of numbers and have them sorted, but when I set it up like this:

alias st 'echo $1 |tr ',' '\n'|sort -n|paste -s -d',' '

And run it, I get the following error.

paste: 8,56,15,37,21: No such file or directory

Is there a way to have the alias recognize the single quotes?
 
Old 04-18-2012, 03:41 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Alias does not work that way, so your approach will not work.

What you need is a shell function; just consider it another type of alias. Try this:
Code:
st () {
    echo "$*" | tr '\t\n\v\f\r ,' '\n\n\n\n\n\n\n' | sort -n | paste -s -d ','
}

Last edited by Nominal Animal; 04-18-2012 at 03:42 PM.
 
1 members found this post helpful.
Old 04-18-2012, 03:44 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Aliases do not use parameters, but you could create a function in your .bashrc (assuming bash) and call it as required.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to Sort Columns of Data (descending to ascending)? msbstar Linux - Newbie 5 03-21-2011 12:08 PM
Ascending or descending order for poll results? linuxlover.chaitanya LQ Suggestions & Feedback 3 02-09-2011 11:24 PM
Can I use GNU sort to sort one field in order, another in reverse? zombieite Linux - Newbie 4 04-27-2009 12:23 AM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 10:12 AM
ascending order huno Programming 4 07-22-2005 07:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration