LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux > 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

Tags used in this thread
Popular LQ Tags , , , , , , , , ,

Reply
 
Thread Tools
Old 05-27-2009, 04:11 AM   #16
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,810
Blog Entries: 5
Thanked: 114

[Log in to get rid of this advertisement]
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.
ghostdog74 is offline     Reply With Quote
Thanked by:
Old 05-28-2009, 05:38 AM   #17
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 9
Thanked: 0

Original Poster
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
$
iframe is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 09:14 AM
Print only specific rows in a text file Mike_V Programming 3 04-24-2009 08:18 PM
Compare two fields on consecutive rows and print the two rows aditi_borkar Linux - Newbie 3 04-09-2009 06:49 AM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 12:57 PM


All times are GMT -5. The time now is 10:10 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration