Sponsored Content
Full Discussion: ksh : split hex number group
Top Forums Shell Programming and Scripting ksh : split hex number group Post 302974982 by Don Cragun on Tuesday 7th of June 2016 12:16:42 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 09:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy