|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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-fileThere 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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Similar post : http://www.unix.com/shell-programmin...y-awk-any.html
|
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
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 38Script 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.txtLast 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 | ||
|
![]() |
| Tags |
| numbers, sorting |
| Thread Tools | Search this Thread |
| 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 |
|
|