1834204 Members
2153 Online
110066 Solutions
New Discussion

Re: disk sorting

 
SOLVED
Go to solution
andi_1
Frequent Advisor

disk sorting

Hi guys,

Suppose, I have the following disks:
c1t0d0
c2t0d0
c3t1d0
c4t0d0
c3t0d0
c1t0d2
c1t2d0

basic format is : cXtYdZ
and I want to sort using first X, then Y and then Z.
The following sort does a good job:
sort -t 'd' -k 1,2n -k1.1b,1.2b diskCollection

until, I have X as a double digit, e.g.
c11t6d0...

it puts c11t6d0 in front for reason.

Does anyone know how to force it for sort in a correct way?

Thank you!

14 REPLIES 14
harry d brown jr
Honored Contributor
Solution

Re: disk sorting



cat diskCollection | sed "s/^c\(.*\)t\(.*\)d\(.*\)/\1 \2 \3/" | sort -t" " -k1n -k2n -k3n|sed -e "s/^/c/" -e "s/ /t/" -e "s/ /d/"


live free or die
harry
Live Free or Die
Bill McNAMARA_1
Honored Contributor

Re: disk sorting

yowser!

You could try to create the list with ll /dev/dsk/ sorted.

Later,
Bill
It works for me (tm)
Bill McNAMARA_1
Honored Contributor

Re: disk sorting

oops, that what that was!

aahh,
Perhaps, ioscan could do it:

for i in $(ioscan -fnkC ext_bus | grep ext_bus | cut -d" " -f4)
> do
> ls /dev/dsk/c${i}t* | grep -v not
> done

on second thoughts, then there's the target to add in there in a second loop, it's easier to stick with Harry solution afterall!

Later,
Bill

It works for me (tm)
Jordan Bean
Honored Contributor

Re: disk sorting


I used this posix script recently.

#!/usr/bin/sh
ls -1 /dev/dsk | while IFS="ctd${IFS}" read ignore1 c t d ignore2
do
print $c $t $d
done | sort -n -k1n -k2n -k3n | while read c t d ignore
do
print c${c}t${t}d${d}
done


As you can see, I thorougly enjoy using the 'while read' method where possible.
andi_1
Frequent Advisor

Re: disk sorting

Hi Harry,

Your solution worked perfectly.

Unfortunately, I will have disk in the file in this kind of format, and the code you showed didn't actually work for this format.

c1t8d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c0t9d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 1
c0t11d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 2

do you know what is needed to be changed in your code?

Thank you very much!
Rodney Hills
Honored Contributor

Re: disk sorting

You can change Harry's script to-

cat diskCollection | sed "s/^c\(.*\)t\(.*\)d\(\[0-9\]*\)/\1 \2 \3/" | sort -t" " -k1n -k2n -k3n|sed -e "s/^/c/" -e "s/ /t/" -e "s/ /d/"

This is by changes (.*) to ([0-9]*) means to scan only for numerics following the "d".

-- Rod Hills


There be dragons...
andi_1
Frequent Advisor

Re: disk sorting

Unfortunately, it doesn't seem to work.

cc0t11d0tNONEd4095 JBOD/Other SEAGATE ST34573WC 2
cc0t9d0tNONEd4095 JBOD/Other SEAGATE ST34573WC 1
cc1t8d0tNONEd4095 JBOD/Other SEAGATE ST34573WC 0
harry d brown jr
Honored Contributor

Re: disk sorting

Andi,

I tried your extended example and it still worked. Can you post what you used?

Also, using Rodney's modified because his drops the text, unless that's what you want, anyways here:

Oh wait, you do want to drop the text, correct? If so, use Rodney's. - I'm babnling, I think I need a drink SOON.

this keeps the text:

cat yank | sed "s/^c\(.*\)t\(.*\)d\([0-9]*\)\(.*\)/\1 \2 \3\4/" | sort -t" " -k1
n -k2n -k3n|sed -e "s/^/c/" -e "s/ /t/" -e "s/ /d/"

live free or die
harry

Live Free or Die
harry d brown jr
Honored Contributor

Re: disk sorting

andi,

If you want to "drop" the text use this:

cat yank | sed "s/^c\(.*\)t\(.*\)d\([0-9]*\)\(.*\)/\1 \2 \3/" | sort -t" " -k1n
-k2n -k3n|sed -e "s/^/c/" -e "s/ /t/" -e "s/ /d/"


live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: disk sorting

andi,

this is the file I used:

# cat yank
c1t8d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c0t9d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 1
c0t11d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 2
c1t0d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c1t10d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c1t10d1 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c1t10d5 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c11t6d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c1t10d30 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c2t0d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c3t1d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c4t0d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c3t0d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c1t0d2 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
c1t2d0 NONE 4095 JBOD/Other SEAGATE ST34573WC 0
#

live free or die
harry
Live Free or Die
Rodney Hills
Honored Contributor

Re: disk sorting

If you want to incorporate perl here, try this

cat input | perl -p -e '/^c(\d+)t(\d+)d(\d+)/;$a=100+$1;$b=100+$2;$c=100+$3;s/^/$a$b$c\t/;' | sort -n | cut -f 2-

This will extract the 3 numerics, put them into a fixed format. The output will then be piped to sort and finally to cut to remove the sort key.

hope this helps...

-- Rod Hills
There be dragons...
Bill Hassell
Honored Contributor

Re: disk sorting

Changing Jordan's script just a little, this should work (and lines up the data nicely):

#!/usr/bin/sh
typeset -L9 DISK
while IFS="ctd${IFS}" read C T D RESTOFLINE
do
print $C $T $D "$RESTOFLINE"
done | sort -n -k1n -k2n -k3n | while read C T D REPLY
do
DISK="c${C}t${T}d${D}"
print "$DISK $REPLY"
done


Usage: cat YourFile | ./thisScript


Bill Hassell, sysadmin
andi_1
Frequent Advisor

Re: disk sorting

Hi Harry,

Your script works perfectly.

Unfortunately, if I have alternate path disk instead of NONE, I get some screwed values:
e.g.
c1t8d0 c1t0d1 4095 JBOD/Other
c0t9d0 NONE 4095 JBOD/Other

Do you know how I can ignore the 2nd disk, and make it behave as was NONE?

Here is the script I used:
cat $TMPDIR/DiskSelection.$$ | sed "s/^c\(.*\)t\(.*\)d\(.*\)/\1 \2 \3/" | sort -t" " -k1n -k2n -k3n|sed -e "s/^/c/" -e "s/ /t/" -e "s/ /d/" >

Thanks a lot!
harry d brown jr
Honored Contributor

Re: disk sorting

Andi,

change the "sed" to look for an "N" or a "c" in the second column:

sed "s/^c\(.*\)t\(.*\)d\(.*\) \([Nc].*\)/\1 \2 \3 \4/"

If you want to "drop" the text just omit the " \4"

live free or die
harry
Live Free or Die