sorting ASCII string containing numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sorting ASCII string containing numbers
# 1  
Old 04-02-2009
sorting ASCII string containing numbers

I have the following output where I need to sort the second column numerically (starting with IBMULT3580-TD10 and ending in IBMULT3580-TD123)

Code:
Drv DriveName       
  0 IBMULT3580-TD13 
  1 IBMULT3580-TD18 
  2 IBMULT3580-TD14 
  3 IBMULT3580-TD10 
  4 IBMULT3580-TD11 
  5 IBMULT3580-TD17 
  6 IBMULT3580-TD117
  7 IBMULT3580-TD121
  8 IBMULT3580-TD15 
  9 IBMULT3580-TD112
 10 IBMULT3580-TD115
 11 IBMULT3580-TD111
 12 IBMULT3580-TD116
 13 IBMULT3580-TD12 
 14 IBMULT3580-TD113
 15 IBMULT3580-TD19 
 16 IBMULT3580-TD118
 17 IBMULT3580-TD16 
 18 IBMULT3580-TD123
 19 IBMULT3580-TD119
 20 IBMULT3580-TD120
 21 IBMULT3580-TD110
 22 IBMULT3580-TD114
 23 IBMULT3580-TD122

I'm trying to apply the sort, and the output is getting sorted, but not numerically:

Code:
`some_command` |sort -k 2

  3 IBMULT3580-TD10 
 21 IBMULT3580-TD110
 11 IBMULT3580-TD111
  9 IBMULT3580-TD112
 14 IBMULT3580-TD113
 22 IBMULT3580-TD114
 10 IBMULT3580-TD115
 12 IBMULT3580-TD116
  6 IBMULT3580-TD117
 16 IBMULT3580-TD118
 19 IBMULT3580-TD119
  4 IBMULT3580-TD11 
 20 IBMULT3580-TD120
  7 IBMULT3580-TD121
 23 IBMULT3580-TD122
 18 IBMULT3580-TD123
 13 IBMULT3580-TD12 
  0 IBMULT3580-TD13 
  2 IBMULT3580-TD14 
  8 IBMULT3580-TD15 
 17 IBMULT3580-TD16 
  5 IBMULT3580-TD17 
  1 IBMULT3580-TD18 
 15 IBMULT3580-TD19

I tried adding -n but it produces a rather weird output, seemingly in random order. Any suggestions how to sort this string numerically? I can't figure it out from the sort man page. TIA!
# 2  
Old 04-02-2009
Quote:
Originally Posted by GKnight
I have the following output where I need to sort the second column numerically (starting with IBMULT3580-TD10 and ending in IBMULT3580-TD123)

Code:
Drv DriveName       
  0 IBMULT3580-TD13 
  1 IBMULT3580-TD18 
  2 IBMULT3580-TD14 
  3 IBMULT3580-TD10 
  4 IBMULT3580-TD11 
  5 IBMULT3580-TD17 
  6 IBMULT3580-TD117
  7 IBMULT3580-TD121
  8 IBMULT3580-TD15 
  9 IBMULT3580-TD112
 10 IBMULT3580-TD115
 11 IBMULT3580-TD111
 12 IBMULT3580-TD116
 13 IBMULT3580-TD12 
 14 IBMULT3580-TD113
 15 IBMULT3580-TD19 
 16 IBMULT3580-TD118
 17 IBMULT3580-TD16 
 18 IBMULT3580-TD123
 19 IBMULT3580-TD119
 20 IBMULT3580-TD120
 21 IBMULT3580-TD110
 22 IBMULT3580-TD114
 23 IBMULT3580-TD122

I'm trying to apply the sort, and the output is getting sorted, but not numerically:

Code:
`some_command` |sort -k 2
 
  3 IBMULT3580-TD10 
 21 IBMULT3580-TD110
 11 IBMULT3580-TD111
  9 IBMULT3580-TD112
 14 IBMULT3580-TD113
 22 IBMULT3580-TD114
 10 IBMULT3580-TD115
 12 IBMULT3580-TD116
  6 IBMULT3580-TD117
 16 IBMULT3580-TD118
 19 IBMULT3580-TD119
  4 IBMULT3580-TD11 
 20 IBMULT3580-TD120
  7 IBMULT3580-TD121
 23 IBMULT3580-TD122
 18 IBMULT3580-TD123
 13 IBMULT3580-TD12 
  0 IBMULT3580-TD13 
  2 IBMULT3580-TD14 
  8 IBMULT3580-TD15 
 17 IBMULT3580-TD16 
  5 IBMULT3580-TD17 
  1 IBMULT3580-TD18 
 15 IBMULT3580-TD19

I tried adding -n but it produces a rather weird output, seemingly in random order. Any suggestions how to sort this string numerically? I can't figure it out from the sort man page. TIA!

Try this
Code:
`some_command` |sort -n +1 -3

# 3  
Old 04-02-2009
no go, "sort -n +1 -3" produces the same result at "sort -nk 2", i.e.

Code:
  0 IBMULT3580-TD13 
 10 IBMULT3580-TD115
 11 IBMULT3580-TD111
 12 IBMULT3580-TD116
 13 IBMULT3580-TD12 
 14 IBMULT3580-TD113
 15 IBMULT3580-TD19 
 16 IBMULT3580-TD118
 17 IBMULT3580-TD16 
 18 IBMULT3580-TD123
 19 IBMULT3580-TD119
  1 IBMULT3580-TD18 
 20 IBMULT3580-TD120
 21 IBMULT3580-TD110
 22 IBMULT3580-TD114
 23 IBMULT3580-TD122
  2 IBMULT3580-TD14 
  3 IBMULT3580-TD10 
  4 IBMULT3580-TD11 
  5 IBMULT3580-TD17 
  6 IBMULT3580-TD117
  7 IBMULT3580-TD121
  8 IBMULT3580-TD15 
  9 IBMULT3580-TD112

# 4  
Old 04-02-2009
I'm confused about sort keys - it seems you want the 3580 column as numeric key 1 and the numeric part of TD as numeric key 2 - so here is a kind of tag sort -
Code:
cut -c 11-14,18- filename | paste - filename | sort -n | 
     awk '{print $2, $3, $4}'

# 5  
Old 04-02-2009
Sorry for the confusion, what I'm trying to do is to sort the second column disregarding the "IBMULT3580-TD" string. I was hoping sort would just sort it numerically by last 3 digits. I guess it doesn't work this way?

Is there a way to do it with just sort, without using cut and/or awk?
# 6  
Old 04-02-2009
Do you have the bash shell?

Or
Code:
sort -n -k2.14,2.18 filename

# 7  
Old 04-02-2009
Hammer & Screwdriver what about

Code:
> sort +1.14n file10
  3 IBMULT3580-TD10 
  4 IBMULT3580-TD11 
 13 IBMULT3580-TD12 
  0 IBMULT3580-TD13 
  2 IBMULT3580-TD14 
  8 IBMULT3580-TD15 
 17 IBMULT3580-TD16 
  5 IBMULT3580-TD17 
  1 IBMULT3580-TD18 
 15 IBMULT3580-TD19 
 21 IBMULT3580-TD110
 11 IBMULT3580-TD111
  9 IBMULT3580-TD112
 14 IBMULT3580-TD113
 22 IBMULT3580-TD114
 10 IBMULT3580-TD115
 12 IBMULT3580-TD116
  6 IBMULT3580-TD117
 16 IBMULT3580-TD118
 19 IBMULT3580-TD119
 20 IBMULT3580-TD120
  7 IBMULT3580-TD121
 23 IBMULT3580-TD122
 18 IBMULT3580-TD123

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert ascii character values to number that comes between the numbers in UNIX

I have variable that contains multiple values of number and also include overpunch(i.e. # $ % etc) character so we want to replace it with numbers. here are the example: Code: 11500#.0# 28575$.5$ 527#.7# 42".2" 2794 .4 2279!.9! 1067&.7& 926#.6# 2279!.9! 885".5" 11714$.4$ 27361'.1'... (1 Reply)
Discussion started by: nadeemrafikhan
1 Replies

2. OS X (Apple)

Sorting scientific numbers with sort

Hey everybody, I'm trying to sort scientific numbers in a descending order using the command sort -gr <file>. It works fine on a Linux-Server, but doesn't on my macbook pro with OS X 10.10.3 (Yosemite). I tried to sort the following: 6.38e-10 6.38e-10 1.80e-11 1.00e-10 1.48e-12 And... (9 Replies)
Discussion started by: plebs
9 Replies

3. Shell Programming and Scripting

Help with Sorting numbers in a file

Hi Guys, Would appreciate some help on sorting numbers on a file using the sort command. I have tried this and it's not sorting properly. what am i missing? cat testing_sort 1:21 4:18 2:17 7:14 9:19 3:12 0:16 8:13 5:20 6:15 10:11 sort -t: -nk1,1 -nk2,2 testing_sort (4 Replies)
Discussion started by: Apollo
4 Replies

4. Shell Programming and Scripting

Sorting mixed numbers and letters

Hello, I have a file such as this: chr1 chr2 chr1 chr2 chr3 chr10 chr4 chr5 chrz chr1AI want to sort it, I use this command: sort -k1 -th -n testfilebut I get this output, how can I fix this? chr1 chr1 chr10 chr1A chr2 chr2 (3 Replies)
Discussion started by: Homa
3 Replies

5. Shell Programming and Scripting

Sorting numbers containing symbols

hi all, i need help on sorting data. i have a file as below /home/oracle $ cat 234.txt +1234 -2356 -1001 +231 0023 -0987 +19000 65487 6 after sorting i want the output as below -2356 -1001 (2 Replies)
Discussion started by: mahesh1987
2 Replies

6. UNIX for Dummies Questions & Answers

sorting numbers with sort -n

Looking for help for sort, I learned that for sorting numbers I use: sort -n but it seems that that is not enough when you have numbers like 0.2000E+7 for example, sort -n will not worry about the E+7 part, and will just sort the numbers like 0.2000. Exapmle: cat example.txt .91000E+07... (9 Replies)
Discussion started by: cosmologist
9 Replies

7. UNIX for Dummies Questions & Answers

Sorting data in an ASCII file

Hi,,, is there anyway to sort the data that I have on an ASCII file, using unix? :confused::confused::confused: Thanks (2 Replies)
Discussion started by: cosmologist
2 Replies

8. Shell Programming and Scripting

ascii sorting in unix

Hi all i am facing a problem in sorting command. The script depending on the sorting command works fine only if ascii sorting is done. i need to know how to find out how to perform ascii sorting. sorting is case insensitive in my file has data in the following format. AA/BB/ AAA/BB\ also... (1 Reply)
Discussion started by: sais
1 Replies

9. UNIX for Dummies Questions & Answers

sorting an ascii file

I would like to sort a large ascii file. Primary sort on the 1st column, and secondary sort on the second column. The file consists of 10 rows of header text and then thousands of rows with 6 columns. I want to sort all of the rows by the first column and second column. As an example, here is... (2 Replies)
Discussion started by: shes
2 Replies

10. Shell Programming and Scripting

Trouble sorting numbers embedded

Hi All, First time poster. I have a text file that as many entries like below (single line): egrep RspTime conlb.txt |more S(xxx.xxx.xxx.xxx:5050:UP) Hits(13358983, 2/sec, P) ATr(2) Mbps(0.07) BWlmt(0 kbits) RspTime(16.50 ms) ... I am trying to sort on the RspTime from highest to lowest... (4 Replies)
Discussion started by: mycrew2008
4 Replies
Login or Register to Ask a Question