LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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

Reply
 
Search this Thread
Old 05-27-2009, 03:11 AM   #16
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241

Quote:
Originally Posted by PMP View Post
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.
 
Old 05-28-2009, 04:38 AM   #17
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
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 View Post
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
$
 
  


Reply

Tags
can, function, join, string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to duplicate rows of text from one file to another? guest Programming 1 04-25-2009 08:14 AM
Print only specific rows in a text file Mike_V Programming 3 04-24-2009 07:18 PM
Compare two fields on consecutive rows and print the two rows aditi_borkar Linux - Newbie 3 04-09-2009 05:49 AM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 11:57 AM


All times are GMT -5. The time now is 07:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration