Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expand hex list 1

Status
Not open for further replies.

mrn

MIS
Apr 27, 2001
3,993
GB
All,

Got an interesting problem to deal with, I'm creating part of an autobuild script (in ksh).

We receive a range of disk metas that will be assigned to a AIX vio server.

0733 - 07D4

I need a way of creating a list from the values above.

I was thinking of converting them to dec then using count+1 from the first value to the last, then converting the list back to hex. This would work but for the added complexity of having a list like

0733 - 07D4 + 08E6 - 08E7

Does anyone know / think of another method.

Regards.

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Hi

Iterating could be done with [tt]seq[/tt] :
Code:
from='0733'
to='07D4'
seq $(( ${from/#0/0x} )) $(( ${to/#0/0x} )) | xargs printf '%04X\n'
Yes, I know, by default there is no [tt]seq[/tt] on Unixes. But maybe you have it from other source. If not, I see no other way then the "manual" looping :
Code:
i=$(( ${from/#0/0x} ))
while (( i<=${to/#0/0x} )); do
  printf '%04X\n' $(( i++ ))
done
Regarding the sample data, I can not get its meaning. Could you explain this, please :
Mike said:
0733 - 07D4 + 08E6 - 08E7

Feherke.
 
The range of meta's is 0733 to 07D4 and 08E6, 08E7.

So basically the meta's may not always be concurrent.



Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Hi

You mean two ranges ? Range 0733..07D4 followed by range 08E6..08E7 ? I think you will have to expand them separately. And this is the moment when I would start writing functions :
Code:
function expandrange
{
  i=$(( ${1/#0/0x} ))
  while (( i<=${2/#0/0x} )); do
    printf '%04X\n' $(( i++ ))
  done
}

function expandline
{
  echo "$1" | tr '+' '\n' | while IFS='-' read from to; do
    expandrange $from $to
  done
}

str='0733 - 07D4 + 08E6 - 08E7'

expandline "$str"
Tested with [tt]mksh[/tt] ( MirBSD Korn shell ).

Feherke.
 
Thanks Feherke,

But I don't have mksh I've got to use ksh (Don't ask my why).....

The Substitution ${1/#0/0x} isn't valid...

But thanks for the idea, It's given me another direction to go down.




Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Hi

Mike said:
The Substitution ${1/#0/0x} isn't valid...
That is [tt]ksh[/tt]93. So you have [tt]ksh[/tt]88.

But no problem, is enough to just add the "0x" prefix without removing the leading "0".
Code:
function expandrange
{
  i=$(( [red]0x$1[/red] ))
  while (( i<=[red]0x$2[/red] )); do
    printf '%04X\n' $(( i++ ))
  done
}

Feherke.
 
Many thanks 0x$1 didn't work but I can get away with using ksh93.



Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top