Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-02-2012
Registered User
 
Join Date: Jul 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Quick Question: Sorting

Hi fellow linuxers

I have a quick question... I would like to sort the numbers in each line that are within a file, and I want to sort them so that the numbers in each line go from SMALLEST to HIGHEST. For example, my file looks like this:


Code:
6 4 2 3 1 5 7 8
15 16 11 13 12 14 20 18 19 17
24 26 25 21 22 23 
34 32 31 36 35 33 38 37


and I want it to look like this:

Code:
1 2 3 4 5 6 7 8
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26
31 32 33 34 34 36 37 38

Any advice on how I can do this?? Thanks everyone!

lucshi09

Last edited by radoulov; 07-03-2012 at 03:57 AM..
Sponsored Links
    #2  
Old 07-02-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,454
Thanks: 108
Thanked 498 Times in 477 Posts
I don't know how efficient this is, but it might work well enough if your files are small:


Code:
awk '
  {
     split( $0, a, " " ); 
     asort( a ); 
     for( i = 1; i <= length( a ); i++ ) 
        printf( "%d ", a[i] );
      printf( "\n" ); 
   }
' input-file >output-file

There used to be a bug in asort , so go with caution. Also, asort is a gnu extension (I believe) so it might not be available. You could write a small sort function in the awk programme; again I don't know how efficient that is. I use a small bubble sort function, to avoid asort, but only in conjunction with small tasks because of a concern for efficiency.

EDIT: I ran a quick test to sort 25 values per line, over 100,000 lines. It took 10.6s on my not so speedy laptop.

Last edited by agama; 07-02-2012 at 10:37 PM.. Reason: Additional info.
Sponsored Links
    #3  
Old 07-03-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts

Code:
 
$ perl -lane '@a=sort @F;print join " ",@a' input.txt
1 2 3 4 5 6 7 8
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26
31 32 33 34 35 36 37 38

    #4  
Old 07-03-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,561
Thanks: 14
Thanked 438 Times in 423 Posts

Code:
perl -lane 'print join " ", sort {$a <=> $b} @F' input.txt

@itkamaraj: Numerical sorting :-)
The Following User Says Thank You to balajesuri For This Useful Post:
itkamaraj (07-03-2012)
Sponsored Links
    #5  
Old 07-03-2012
Registered User
 
Join Date: Jul 2012
Posts: 30
Thanks: 0
Thanked 1 Time in 1 Post

Code:
tr -s ' ' '\n' < filename | sort | tr -s '\n' ' '

if it is not working, try to read one by one line using loop and use the above command

Last edited by radoulov; 07-03-2012 at 03:57 AM..
Sponsored Links
    #6  
Old 07-03-2012
jayan_jay's Avatar
Forum Advisor
 
Join Date: Jul 2008
Posts: 831
Thanks: 9
Thanked 185 Times in 176 Posts
Similar post : http://www.unix.com/shell-programmin...y-awk-any.html
Sponsored Links
    #7  
Old 07-03-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
Further to post #5 , you'd need to read the file line-by-line, then sort the contents of the line. Also lose the "-s" switch to "tr".


Code:
cat filename.txt | while read line
do
        sorted=`echo "${line}" | tr " " "\n"| sort -n | tr "\n" " "`
        echo "${sorted}" | sed 's/ $//g'
done

./scriptname
1 2 3 4 5 6 7 8
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26
31 32 33 34 35 36 37 38

Script has a flaw because it introduces a trailing space character on each record, hence the sed to remove that character.


If you have a modern Shell, the newer syntax is preferred:

Code:
while read line
do
        sorted=$(echo "${line}" | tr " " "\n"| sort -n | tr "\n" " ")
        printf "${sorted}\n" | sed 's/ $//g'
done < filename.txt


Last edited by methyl; 07-03-2012 at 07:30 AM.. Reason: alternate syntax
The Following User Says Thank You to methyl For This Useful Post:
figaro (07-03-2012)
Sponsored Links
Closed Thread

Tags
numbers, sorting

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
Quick question regarding IFS DeCoTwc UNIX for Dummies Questions & Answers 1 05-11-2010 11:11 PM
Quick question. raidkridley UNIX for Dummies Questions & Answers 2 11-11-2008 12:31 PM
Quick question JohnnyBoy UNIX for Dummies Questions & Answers 2 11-03-2006 07:23 AM
Another quick question hamoudzz UNIX for Dummies Questions & Answers 1 04-07-2004 10:06 PM
quick question hamoudzz UNIX for Dummies Questions & Answers 2 04-06-2004 11:18 AM



All times are GMT -4. The time now is 02:43 AM.