Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting case 1

Status
Not open for further replies.

ogniemi

Technical User
Nov 7, 2003
1,041
PL

I have input with single filed in each line, like.

$ echo "aaa1\naaa2\nbbbbb0\naaa11\nbbbbb10\nbbbbb1\nbbbbb8\naaa9"
aaa1
aaa2
bbbbb0
aaa11
bbbbb10
bbbbb1
bbbbb8
aaa9

and can do "by number" sorting in that way:

$ echo "aaa1\naaa2\nbbbbb0\naaa11\nbbbbb10\nbbbbb1\nbbbbb8\naaa9"|sort -kn1.4 -kn1.6
bbbbb0
bbbbb1
bbbbb8
bbbbb10
aaa1
aaa2
aaa9
aaa11

Is there a universal command for any length of leading text + a number?
 
Since the numeric part starts at a different location in different lines, I think you need a delimiter to do what you want. This works, but it feels like cheating. [bigsmile] Maybe that's ok.

Code:
$ echo "aaa1\naaa2\nbbbbb0\naaa11\nbbbbb10\nbbbbb1\nbbbbb8\naaa9" | sed 's/^[a-z]*/& /' | sort -k2 -n | sed 's/ //'
bbbbb0
aaa1
bbbbb1
aaa2
bbbbb8
aaa9
bbbbb10
aaa11
$

Basically it adds a space to delimit the fields, sorts them, then removes the space.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top