Quick Question: Sorting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Quick Question: Sorting
# 1  
Old 07-02-2012
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 04:57 AM..
# 2  
Old 07-02-2012
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 11:37 PM.. Reason: Additional info.
# 3  
Old 07-03-2012
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
Code:
perl -lane 'print join " ", sort {$a <=> $b} @F' input.txt

@itkamaraj: Numerical sorting :-)
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 07-03-2012
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 04:57 AM..
# 6  
Old 07-03-2012
# 7  
Old 07-03-2012
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 08:30 AM.. Reason: alternate syntax
This User Gave Thanks to methyl For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Quick question

Hi guys Quick question Im creating an FTP server and im chrooting each user to there home directory blah blah. Ive also setup scponly so there locked etc. Im a novice at unix and have just reaslised the primary group of scponly is the username of one of the ftp users... which im sure... (1 Reply)
Discussion started by: mokachoka
1 Replies

2. Shell Programming and Scripting

quick question

I am using sed to find a pattern in a line and then I want to retain the pattern + the rest of the line. How is this possible? ie: line is: 14158 05-15-08 20:00 123-1234-A21/deliverable/dhm.a search for 123-1234-A21 ie: echo $line | sed 's/.*\(\{3\}-\{4\}-\{3\}\{5\}\).*/\1/' ... (1 Reply)
Discussion started by: phreezr
1 Replies

3. AIX

quick question

Hi, At best I'm a junior admin with a big problem. My developers have got my root password and mgmt insists they need it. I can't even change it when people knowing it leave. I'm certain they've hardcoded it into routines. I've searched my servers and grepped everything & can't find it. ... (5 Replies)
Discussion started by: keith.m
5 Replies

4. UNIX for Dummies Questions & Answers

Quick question

Hello all, Quick question from a fairly new to Unix developer. if then completedLogFile=$logfile.$(date +%Y%m%d-%H:%M:%S) mv $logfile $completedLogFile fi I understand that this portion of code is simply copying a tmp logfile to a completed logfile when a condition is true. The... (2 Replies)
Discussion started by: JohnnyBoy
2 Replies

5. UNIX for Dummies Questions & Answers

Quick question

Hi, Is there a simple way, using ksh, to find the byte position in a file that a stated character appears? Many thanks Helen (2 Replies)
Discussion started by: Bab00shka
2 Replies

6. UNIX for Dummies Questions & Answers

Quick Question

Hi, I am new to UNIX, and am learning from this tutorial : http://www.ee.surrey.ac.uk/Teaching/Unix/index.html It keeps telling me to files downloaded from the internet (like .txt files) to the directory, and I dont know how to. How do I add .txt files to my directory? Thanks. (6 Replies)
Discussion started by: IAMTHEEVILBEAN
6 Replies

7. Shell Programming and Scripting

quick question

does anyone know what $? means? i echoed it on my box (running AIX Korn shell) and got 127 (2 Replies)
Discussion started by: penfold
2 Replies

8. UNIX for Advanced & Expert Users

Quick VI question

This "SHOULD" be a simple question, but looking through several books has turned up nothing, so I turn once again to the experts!! How do you vi a file so that you can see special characters. I believe my /etc/passwd file is being corrupted during an upgrade process, however the files... (6 Replies)
Discussion started by: Recon
6 Replies

9. UNIX for Dummies Questions & Answers

Quick Question

Hello There! I am trying to write this SIMPLE script in Bourne Shell but I keep on getting syntax errors. Can you see what I am doing wrong? I've done this before but I don't see the difference. I am simply trying to take the day of the week from our system and when the teachers sign on I want... (7 Replies)
Discussion started by: catbad
7 Replies

10. UNIX for Dummies Questions & Answers

Quick Question

I know in DOS, when you want to pull up your last/previous command, you hit the up/down arrows. How do you do that with UNIX? (3 Replies)
Discussion started by: Tracy Hunt
3 Replies
Login or Register to Ask a Question