Operating System - Linux
1753666 Members
6219 Online
108799 Solutions
New Discussion юеВ

Best and fast way to sort cXtYdZ values picking the lowest "c" value?

 
Alzhy
Honored Contributor

Best and fast way to sort cXtYdZ values picking the lowest "c" value?

I'm dealing with hundreds/thousands of cXtYdZ values on a typical server and I have a need in my script to pick from a list of cXtYdZ the lowest numbered cX, then the lowest numbers tY then the lowest numbered dZ -- in effect the lowest numbered/valued cXtYdZ.

Suggestions? More points to Perl constructs.
Hakuna Matata.
11 REPLIES 11
Steven E. Protter
Exalted Contributor

Re: Best and fast way to sort cXtYdZ values picking the lowest "c" value?

Shalom Nelson,

Perl from someone else

ls | /dev/dsk/c* | sort -rn

take out the r if it comes out backwards.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Rick Garland
Honored Contributor

Re: Best and fast way to sort cXtYdZ values picking the lowest "c" value?

perl or no perl, you still run it through the 'sort' command

In perl, make a call to a system command

Bharat Katkar
Honored Contributor

Re: Best and fast way to sort cXtYdZ values picking the lowest "c" value?

ioscan -funC disk | grep c | grep -v Class | awk '{ print }'

Does this output not sorted.
If i understood the question the output of the command above is what you are expecting.

Regards
You need to know a lot to actually know how little you know
Bharat Katkar
Honored Contributor

Re: Best and fast way to sort cXtYdZ values picking the lowest "c" value?

sorry typo. it's like this:
# ioscan -funC disk | grep c | grep -v Class | awk '{ print $1}'

Regards
You need to know a lot to actually know how little you know
Hein van den Heuvel
Honored Contributor

Re: Best and fast way to sort cXtYdZ values picking the lowest "c" value?

The problem with a straighy sort is that for example t10d sorts before t2d.
So you need to pull apart the X, Y and Z.

Now you can create a perl sort function that first compares X then Ys, then Zs, or you can cheat a little.
Create a fresh number by multiplying X by a large range (like 1000... but take your pick), then add Y and mutliply again, and finally add Z.
Now compare numerically.
Pick any number larger than the largest X and Y, but pikcing 1000 or 100 reads nicely.

Sample script to get you going:

------ sort_dsk.pl -----
while (<>) {
if (/dsk.c(\d+)t(\d+)d(\d+)\s/) {
$num = ($1*1000 +$2)*1000+$3;
$dsk{$num} = $_;
}
}
for $key (sort {$a <=> $b} keys %dsk) {
print $key, $dsk{$key};
}
-------------

As input example I used the data from the last reply in: http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=718178

This then gives:

$ perl sort_dsk.pl tmp.tmp
3000000/dev/rdsk/c3t0d0 0 0 CL1F 1:71 OPEN-3 2347 00030107 5247
3000001/dev/rdsk/c3t0d1 0 1 CL1F 1:72 OPEN-3 2347 00030107 5247
3000002/dev/rdsk/c3t0d2 0 2 CL1F 1:73 OPEN-3 2347 00030107 5247

:
6004001/dev/rdsk/c6t4d1 4 1 CL1F 2:7e OPEN-9 7042 00035807 5244
6004002/dev/rdsk/c6t4d2 4 2 CL1F 2:7f OPEN-9 7042 00035807 5244
6009000/dev/rdsk/c6t9d0 9 0 CL1F 3:2c OPEN-9 0 00035807 5244
6010000/dev/rdsk/c6t10d0 10 0 CL1F 2:2e OPEN-9 0 00035807 5244
6011000/dev/rdsk/c6t11d0 11 0 CL1F 1:80 OPEN-9 0 00035807 5244
6012000/dev/rdsk/c6t12d0 12 0 CL1F 1:88 OPEN-9 0 00035807 5244
6013000/dev/rdsk/c6t13d0 13 0 CL1F 3:74 OPEN-9 0 00035807 5244

Enjoy!

Hein.
James R. Ferguson
Acclaimed Contributor

Re: Best and fast way to sort cXtYdZ values picking the lowest "c" value?

Nelson:

# perl -nle 'm/c(\d+)t(\d+)d(\d+)/;printf "c%02dt%02dd%02d\n",$1,$2,$3' file|sort

Regards!

...JRF...
Alzhy
Honored Contributor

Re: Best and fast way to sort cXtYdZ values picking the lowest "c" value?

# perl -nle 'm/c(\d+)t(\d+)d(\d+)/;printf "c%02dt%02dd%02d\n",$1,$2,$3' list|sort
c01t03d00
c111t03d00
c20t03d00
c211t03d00
c21t03d00


CLose but it adds leading zeroes...
Hakuna Matata.
Alzhy
Honored Contributor

Re: Best and fast way to sort cXtYdZ values picking the lowest "c" value?

The result I'd like from the test list should be:

c1t3d0
c111t3d0
c20t3d00
c21t3d0
c211t3d0
Hakuna Matata.
James R. Ferguson
Acclaimed Contributor

Re: Best and fast way to sort cXtYdZ values picking the lowest "c" value?

Hi (again) Nelson:

Of course:

# cat listdevs
#!/usr/bin/perl
while (<>) {
m/c(\d+)t(\d+)d(\d+)/;
$dev = sprintf "c%04dt%04dd%04d\n",$1,$2,$3;
push @ary, $dev;
}
@sorted = sort @ary;
foreach (@sorted) {
m/c(\d+)t(\d+)d(\d+)/;
printf "c%dt%dd%d\n",$1,$2,$3;
}

...run as ./listdevs file

Regards!

...JRF...