ksh : split hex number group

Hi,

sry for poor english

I have a group of hex number as : 4D40:4D42

I want so split this group in a list as :

4D40,4D41,4D42

i don't know how i can do this in ksh

Thanks

What's your idea for an algorithm to do this?

You don't say where your groups of hex numbers come from. The following seems to work if the groups are given as command line arguments to this script:

#!/bin/ksh
while [ $# -gt 0 ]
do	IFS=':' read low high <<-EOF
		$1
	EOF
	low=$((16#$low))
	high=$((16#$high))
	while [ "$low" -lt "$high" ]
	do	printf '%X,' "$low" 
		low=$((low + 1))
	done
	printf '%X\n' "$high"
	shift
done

For example, if you name this script tester , make it executable:

chmod +x tester

and invoke it with:

./tester 4D40:4D42 a0:b0

it produces the output:

4D40,4D41,4D42
A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,AA,AB,AC,AD,AE,AF,B0

infile:

4D40:4D42
a0:b0
# gnu awk
awk -F: '{for (i=strtonum("0x" $1); i<= strtonum("0x" $2); i++) {$(++j)=sprintf("%x", i)} } 1 ' OFS=, infile

Hi.

Using brace expansion feature of shell:

#!/usr/bin/env ksh
#!/usr/bin/env bash

# @(#) s3       Demonstrate brace expansion, base conversion.

LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

FILE=${1-data1}

p=$( basename $0 ) t1="$Revision: 1.12 $" v=${t1//[!0-9.]/}
[[ $# -gt 0 ]] && [[ "$1" =~ -version ]] &&  { echo "$p (local) $v" ; exit 0 ; }

# Function to convert base-16 to base-10
hex() {
printf "%d\n" "0x$1"
}

pl " Example 1, brace expansion, constants:"
printf '%d,' {2..4} ; printf "\n"

pl " Example 2, variables (bash fails; ksh succeeds):"
lo=3
hi=5
printf '%d,' {$lo..$hi} ; printf "\n"

pl " Example 3, variables, but with eval:"
eval "printf '%d,' {$lo..$hi}" ; printf "\n"

pl " Example 4, hex values in variables, remove final comma:"
lo=a0
hi=b0
eval "printf '%x,' {$(hex $lo)..$(hex $hi)}" | sed '$s/,$/\n/'

pl " Example 5, hex values in variables, arithmetic to eliminate comma:"
lo=a0
hi=b0
eval "printf '%x,' {$(hex $lo)..$(($(hex $hi)-1))}" ; printf "%x\n" $(hex $hi)

pl " Example 6, read from file, create sequence:"
IFS=":" read lo hi < $FILE
pe " Data:"
printf " lo = %s, hi = %s\n" $lo $hi
eval "printf '%x,' {$(hex $lo)..$(($(hex $hi)-1))}" ; printf "%x\n" $(hex $hi)

exit 0

producing:

$ ./s3

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
bash GNU bash 4.3.30

-----
 Example 1, brace expansion, constants:
2,3,4,

-----
 Example 2, variables (bash fails; ksh succeeds):
3,4,5,

-----
 Example 3, variables, but with eval:
3,4,5,

-----
 Example 4, hex values in variables, remove final comma:
a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,ae,af,b0

-----
 Example 5, hex values in variables, arithmetic to eliminate comma:
a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,ae,af,b0

-----
 Example 6, read from file, create sequence:
 Data:
 lo = 4D40, hi = 4D42
4d40,4d41,4d42

See man pages, Google for details.

Best wishes ... cheers, drl

I don't use gnu awk , and the awk I use doesn't have strtonum() as a built-in; but I don't see how this is going to work. Since j starts with a default value of 0, the 2nd time through the loop for the 1st input line is going to reset the end of the loop value initially presented as input $2 value. And, since j isn't reset between lines, subsequent output lines will copy the start and end points as the first two output fields in the output , followed by a number of empty fields that varies depending on how many fields have been output on the previous lines, followed by the desired output. If you want to do this using awk instead of ksh , it would seem that the following might work better:

awk -F: '
{	h=("0x"$2)+0
	j=0
	for (i = ("0x"$1) + 0; i <= h; i++)
		$(++j) = sprintf("%X", i)
}
1' OFS=, infile

Note that I used %X instead of %x in the sprintf() format string because the submitter seems to want uppercase hex digits instead of lowercase.
As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .