Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
05-27-2009, 03:11 AM
|
#16
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by PMP
And if you have perl
do this :-)
cat test.txt
1,5,7,8,3,6
10,34,67,1,2,0,5
command line
perl -e 'while(<>){chomp; my @sorted = sort { $a <=> $b } split(",", $_); print join (",", @sorted); print "\n"}' test.txt
1,3,5,6,7,8
0,1,2,5,10,34,67
Cheers
|
note there is "x" in front of the numbers. also, Perl options -n simulates the while <> loop, so it can be omitted.
|
|
|
|
05-28-2009, 04:38 AM
|
#17
|
|
LQ Newbie
Registered: Mar 2007
Posts: 15
Original Poster
Rep:
|
Improvement
In order to make this script more portable, I modified it as follows
Code:
infile=$1
for i in $(cat $infile)
do
echo $i | tr "," "\n" | sort -n | tr "\n" "," | sed "s/,$//"
echo
done
save it as: sort_file
usage: sort_file < sort_me.csv > outfile.csv
Thanks to all for your time and interest.
Quote:
Originally Posted by Admiral Beotch
Code:
for i in $(cat /tmp/numbers.txt);
do
echo $i | tr "," "\n" | sort -n | tr "\n" "," | sed "s/,$//"
echo
done
$ cat /tmp/numbers.txt
4,2,5,3,1,6
9,7,10,8,11
$ /tmp/testnumbers
1,2,3,4,5,6
7,8,9,10,11
$
|
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:24 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|