ksh : split hex number group


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh : split hex number group
# 1  
Old 06-06-2016
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
# 2  
Old 06-06-2016
What's your idea for an algorithm to do this?
# 3  
Old 06-06-2016
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:
Code:
#!/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:
Code:
chmod +x tester

and invoke it with:
Code:
./tester 4D40:4D42 a0:b0

it produces the output:
Code:
4D40,4D41,4D42
A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,AA,AB,AC,AD,AE,AF,B0

# 4  
Old 06-06-2016
infile:
Code:
4D40:4D42
a0:b0

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

# 5  
Old 06-07-2016
Hi.

Using brace expansion feature of shell:
Code:
#!/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:
Code:
$ ./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

Last edited by drl; 06-07-2016 at 01:08 AM..
# 6  
Old 06-07-2016
Quote:
Originally Posted by rdrtx1
infile:
Code:
4D40:4D42
a0:b0

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

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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using sed to split hex string

Hi, I'm looking to split the following hex string into rows of four elements. I've tried the following but it doesn't seem to work. How can I tell sed to match based on a pair of number(s) and letter(s), and add a newline every 4 pairs? In addition, I need to add another newline after every... (5 Replies)
Discussion started by: sand1234
5 Replies

2. Shell Programming and Scripting

Hex number sequence help

Need some help doing this ... with awk maybe Input 0DF6 0DF7 0DF8 0DF9 0DFA 0DFB 0DFC 0DFD 0DFF 0E00 0E01 0E02 0E03 0E04 0E05 0E06 (11 Replies)
Discussion started by: greycells
11 Replies

3. Shell Programming and Scripting

Convert Hex - KSH

Hello, I woild like to convert hex on KSH not BASH: I tried to use: tmp=31 printf "\x"${tmp}"" it works on bash - Output is '1' but not on ksh. please advice on the right syntax. Thanks. (4 Replies)
Discussion started by: LiorAmitai
4 Replies

4. Shell Programming and Scripting

awk to compare hex number

$ awk 'BEGIN{ pat111=0x1000000002E3E02; snBegin=0x1000000002E3E01; if (pat111<=snBegin) printf "a\n"}' a Result is not correct. Looks like the number is too big. Any idea? Thx! Please use code tags <- click the link! (2 Replies)
Discussion started by: carloszhang
2 Replies

5. Shell Programming and Scripting

awk to remove leading zeros for a hex number

Is it possible by using awk to remove leading zeros for a hex number? ex: 0000000011179E0A -> 11179E0A Thank you! (4 Replies)
Discussion started by: carloszhang
4 Replies

6. Shell Programming and Scripting

HEX number grouping

Guys, I am looking for a small script which generates HEX sequence. Input to the script is starting hex number - Group ID and number of members in a group and total groups. e.g: we are generating 2 groups with 4 Members each starting with hex number 036A. I should get o/p in following format. ... (5 Replies)
Discussion started by: dynamax
5 Replies

7. Shell Programming and Scripting

Split these into many ...(/etc/group)!!

Guys Following input line is from /etc/group file.As we know last entry in a line of /etc/group is userlist (all the users belonging to that group). I need to splilt this one line into 3 lines as shown below (3 because userlist has 3 names in it). Input: lp:!:11:root,lp,printq ... (13 Replies)
Discussion started by: ak835
13 Replies

8. Shell Programming and Scripting

incremental addition of hex decimal number in one field

Hi I want to incremental add hex decimal number to a particula field in file eg: addr =123 dept1=0 addr = 345 dept2 =1 addr2 = 124 dept3 =2 . . . . . . addr3 =567 dept15 =f Is there any command which add... (8 Replies)
Discussion started by: diddi_linux
8 Replies

9. UNIX for Advanced & Expert Users

retrieving all group names with a given group number

hi, which Unix/C function can i use to retrieve all group names with a particular group id? The following C code prints out the group id number of a particular group name: ------------------------------------------------------------------------ #include <stdio.h> #include <grp.h> int... (3 Replies)
Discussion started by: Andrewkl
3 Replies

10. Shell Programming and Scripting

ksh and hex numbers

typeset -i A=16#0 typeset -u A=$a y=${A#16#} This converted $a to hex and stored it in y. Can someone walk me through how this was done? thanks (2 Replies)
Discussion started by: JamesByars
2 Replies
Login or Register to Ask a Question