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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Alphenumeric

Status
Not open for further replies.

kevmullet

Technical User
Feb 12, 2002
56
GB
i got a list of chars and nums like so:

001
100
555
AAA
BBB
124

i need to sort it so it looks like this

AAA
BBB
001
100
124
555

preferably using the sort command

thanks for any help

K
 
Try the -n option of sort
-n Sorts numeric fields by arithmetic value. A numeric field may contain leading blanks, an optional minus sign, decimal digits, thousands-separator characters, and an optional radix character. Numeric sorting of a field containing any nonnumeric character gives unpredictable results.

sort -n file

This appear to work fine.
 
Well, if you are running linux, sort -g does the trick. If AIX, use sort -n.

Bill.
 
looks good so far but it now looks like this

BBB
AAA
001
100
124
555

im so close yet so far. could it be cos its 2nd column of text?

K
 
looks good so far but it now looks like this

BBB
AAA
001
100
124
555

im so close yet so far. could it be cos its 2nd column of text. heres the full file

X 001 Y
X 100 Y
X 555 Y
X AAA Y
X BBB Y
X 124 Y

so im trying sort -n -k 2.

and it looks like this:

X BBB Y
X AAA Y
X 001 Y
X 100 Y
X 124 Y
X 555 Y

Cheers

K
 
Here are my tests
> cat file1
X 001 Y
X 100 Y
X 555 Y
X AAA Y
X BBB Y
X 124 Y

> sort file1
X 001 Y
X 100 Y
X 124 Y
X 555 Y
X AAA Y
X BBB Y

> sort +0.2 file1
X 001 Y
X 100 Y
X 124 Y
X 555 Y
X AAA Y
X BBB Y

> sort -n +0.2 file1
X AAA Y
X BBB Y
X 001 Y
X 100 Y
X 124 Y
X 555 Y

> sort -r +0.2 file1
X BBB Y
X AAA Y
X 555 Y
X 124 Y
X 100 Y
X 001 Y

What kind of Unix OS are you on?
 
im using my uni's sun os unix server

i'll keep trying

tnx for the help

K
 
heres a test of what i did

X 001 Y
F 100 Y
L 555 Y
D AAA Y
Z BBB Y
Q 124 Y

when i try the -n +0.2 sort it does this:

Z BBB Y
D AAA Y
X 001 Y
F 100 Y
Q 124 Y
L 555 Y

seems as though its sorting the second column by numbers but the ones that are chars are being sorted by the 1st column.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top