Strings in ascending order | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

Strings in ascending order

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-07-2013
siya@ siya@ is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 19 June 2014, 11:50 PM EDT
Posts: 23
Thanks: 10
Thanked 0 Times in 0 Posts
Hammer & Screwdriver Strings in ascending order

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  
Old 01-07-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 15 July 2014, 11:14 AM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
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  
Old 01-07-2013
siya@ siya@ is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 19 June 2014, 11:50 PM EDT
Posts: 23
Thanks: 10
Thanked 0 Times in 0 Posts
It worked!!

---------- Post updated at 02:55 PM ---------- Previous update was at 02:54 PM ----------

Thanks very much!! It worked!!!
    #4  
Old 01-07-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 15 July 2014, 11:14 AM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
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  
Old 01-07-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 July 2014, 4:43 AM EDT
Location: San Jose, CA, USA
Posts: 4,127
Thanks: 160
Thanked 1,411 Times in 1,196 Posts
Quote:
Originally Posted by rdrtx1 View Post
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

Hi rdrtx1,
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  
Old 01-07-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 July 2014, 9:08 PM EDT
Location: Galactic Empire
Posts: 3,354
Thanks: 230
Thanked 1,190 Times in 1,123 Posts
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
Closed Thread

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
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



All times are GMT -4. The time now is 08:58 AM.