|
|||||||
| 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
|
|||
|
|||
|
Hi, I have a sequence which has 30000 strings which looks like this Code:
>string2991 234445 >string224 470561 >string121 675386 >string4098 177229 >string8049 255838 >string8 672382 >string1115 578415 I want it to be arranged in ascending order Code:
>string8 672382 >string121 675386 >string224 470561 >string1115 578415 >string2991 234445 >string4098 177229 >string8049 255838 Can it be done? Please do help. Last edited by Scott; 01-10-2013 at 09:57 AM.. Reason: Code tags |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
try: Code:
awk '{a=$0;sub("^[^0-9]*","",a);b[a$1]=a":::"$0} END{for (i in b) print b[i]}' infile | sort -n | sed 's/.*::://'or Code:
sed 's/^\([^0-9]*\)\([0-9]*\)/\2 :::\1\2/' infile | sort -n | sed 's/.*::://' |
| The Following User Says Thank You to rdrtx1 For This Useful Post: | ||
siya@ (01-07-2013) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
It worked!!
![]() ---------- Post updated at 02:55 PM ---------- Previous update was at 02:54 PM ---------- Thanks very much!! It worked!!! |
|
#4
|
|||
|
|||
|
also, another awk example without the need to pipe to sort and sed: Code:
awk '{a=$1;sub("^[^0-9]*","",a);b[a]=$0; m?(0):(m=a); if (a<m) m=a; if (a>x) x=a;}
END{for (i=m; i<=x; i++) {if (b[i])print b[i]}}' infile |
| The Following User Says Thank You to rdrtx1 For This Useful Post: | ||
siya@ (01-07-2013) | ||
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Quote:
At least on OS X this doesn't work. Since a is created by a call to sub(), it is being treated as a string instead of as a number in the comparisons that set the minimum and maximum values for the range to be printed. I believe that the script is working on OS X as specified by the standards. I haven't tried it on any other system to see if awk behaves differently on other systems. If you change m?(0):(m=a) in your script to a += 0 (which converts a from a string to a number), it works as expected. |
| The Following User Says Thank You to Don Cragun For This Useful Post: | ||
siya@ (01-09-2013) | ||
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
How about: Code:
sort -k1.8n infile Here is the o/p: Code:
# cat infile >string2991 234445 >string224 470561 >string121 675386 >string4098 177229 >string8049 255838 >string8 672382 >string1115 578415 Code:
# sort -k1.8n infile >string8 672382 >string121 675386 >string224 470561 >string1115 578415 >string2991 234445 >string4098 177229 >string8049 255838 |
| The Following User Says Thank You to Yoda For This Useful Post: | ||
siya@ (01-09-2013) | ||
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| File listing in Ascending order | rakesh_arxmind | Shell Programming and Scripting | 2 | 12-17-2012 09:14 AM |
| sorting sequences in ascending order | sonia102 | UNIX for Dummies Questions & Answers | 5 | 09-25-2012 11:41 AM |
| Ascending order | kristinu | Shell Programming and Scripting | 0 | 11-30-2010 01:53 PM |
| Ascending order within text | kerpm | Shell Programming and Scripting | 5 | 08-15-2008 09:39 AM |
| Sort / ascending order | gyik | UNIX for Dummies Questions & Answers | 1 | 03-05-2001 09:08 AM |
|
|