Operating System - HP-UX
1834254 Members
1836 Online
110066 Solutions
New Discussion

ksh scripting question: - 101-105,119,164-16A

 
SOLVED
Go to solution
Stuart Abramson_2
Honored Contributor

ksh scripting question: - 101-105,119,164-16A

I wrote a script to list EMC HyperVolumes in a volume group:

101 102 103 104 105 119 163 164 165 166 167 168 169 16A

For reporting purposes, I'd like to summarize the list like this:

101-105,119,164-16A

If it's sequential, summarize.

Can anyone help me do that? In ksh? or awk?

Stuart
3 REPLIES 3
Rodney Hills
Honored Contributor

Re: ksh scripting question: - 101-105,119,164-16A

Here is a perl routine...

foreach $n (@ARGV,FFFF) {
if (hex($prev)+1 == hex($n)) {
$prev=$n;
} else {
push(@list,($prev == $first ? $first : $first."-".$prev)) if $first;
$first=$n; $prev=$n;
}
}
print join(",",@list),"\n";

Run it by putting the above in a file (like rng.pl) and entering-

perl rng.pl 102 102 103 104 105 119 163 ...

result-
101-105,119,164-16A

HTH

-- Rod Hills
There be dragons...
Robin Wakefield
Honored Contributor
Solution

Re: ksh scripting question: - 101-105,119,164-16A

Hi Stuart,

Here's a ksh version of Rod's script:

=========================
#!/usr/bin/ksh

function add2string
{
if [ $to -eq $from ] ; then
s=$s$hfrom$sep
else
s=$s$hfrom-$hto$sep
fi
}

to=-1
sep=","
typeset -i10 n
for m in $*; do
p=16#${m}
let q=$to+1
if [ $p -eq $q ] ; then
to=$p
hto=$m
else
if [ $from ] ; then
add2string
fi
from=$p
hfrom=$m
to=$p
hto=$m
fi
done
sep=""
add2string
echo $s
===========================

Rod - your script needed a couple of mods to cater for when the first value is 1, and if the range was say, 1a-1c, i.e. $prev == $first would equate them as being equal, e.g.

===========================
$prev=FFFF;
foreach $n (@ARGV,FFFF) {
if (hex($prev)+1 == hex($n)) {
$prev=$n;
} else {
push(@list,(hex($prev) == hex($first) ? $first : $first."-".$prev)) if $first;
$first=$n; $prev=$n;
}
}
print join(",",@list),"\n";
===========================

rgds, Robin
Stuart Abramson_2
Honored Contributor

Re: ksh scripting question: - 101-105,119,164-16A

Thanks to both of you!

Stuart