Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-07-2013
siya@ siya@ is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 3 March 2015, 11:17 PM EST
Posts: 24
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 Unix and Linux 01-07-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 13 January 2015, 10:34 AM EST
Location: Houston, Texas, USA
Posts: 681
Thanks: 0
Thanked 206 Times in 198 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 Unix and Linux 01-07-2013
siya@ siya@ is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 3 March 2015, 11:17 PM EST
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
It worked!! Unix or Linux ImageUnix or Linux Image

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

Thanks very much!! It worked!!!
    #4  
Old Unix and Linux 01-07-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 13 January 2015, 10:34 AM EST
Location: Houston, Texas, USA
Posts: 681
Thanks: 0
Thanked 206 Times in 198 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 Unix and Linux 01-07-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 April 2015, 3:49 AM EDT
Location: San Jose, CA, USA
Posts: 6,141
Thanks: 249
Thanked 2,036 Times in 1,740 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 Unix and Linux 01-07-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 20 April 2015, 6:16 PM EDT
Location: Galactic Empire
Posts: 3,402
Thanks: 240
Thanked 1,216 Times in 1,142 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 01:11 PM.