1834276 Members
3088 Online
110066 Solutions
New Discussion

sorting stuff

 
SOLVED
Go to solution
andi_1
Frequent Advisor

sorting stuff

Hi,

I have an arbitrary input in the following form:
c0t11d2 NONE 4095 JBOD/Other SEAGATE
c0t8d1 NONE 4095 JBOD/Other SEAGATE
c0t9d0 NONE 4095 JBOD/Other SEAGATE

I need to perform sort on device field (dX).
I am thinking of using

sort -k 1.LengthOfTheDevice (e.g. sort -k 1.6) filename

Since, the disk name is of variable length, how can I ensure that I always search based on D value?

Thank you!
4 REPLIES 4
Mladen Despic
Honored Contributor

Re: sorting stuff

sort -t 'd' -n -k2,2
andi_1
Frequent Advisor

Re: sorting stuff

Sorry, I might have been not clear enough. I have another requirement:
Suppose, the following info I have:
c0t0d0 NONE 4095 JBOD/Other SEAGATE
c0t0d1 NONE 4095 JBOD/Other SEAGATE
c1t0d0 NONE 4095 JBOD/Other SEAGATE
c1t1d1 NONE 4095 JBOD/Other SEAGATE

When I sort by d, I also need to make sure that the following output displayed:

c0t0d0
c1t0d0
c0t0d1
c1t1d1

If I were to use the sort like this:
sort -t 'd' -n -k1,6 diskDBt

I'd get the following output:
c0t0d0
c0d0t1
c1t0d0
c1t1d1

Thank you!


Mladen Despic
Honored Contributor

Re: sorting stuff



If we use 'd' as the field separator, then we need sort by the second field - correct?
If so, then try using -k2 instead of -k1,6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: sorting stuff

The suggested solution is very close. If you want to sort by the 'd' portion as the most significant and then then stuff that precedes it using 'd' as a field separator this should work:

sort -t 'd' -k 2,2n -k1,1b infile
If it ain't broke, I can fix that.