HEX number grouping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HEX number grouping
# 1  
Old 12-03-2010
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.

Input: Starting HEX number - 036A, Number of groups - 2, Members in a Group- 4.

Output:
Group ID 036A, members;add members 036B:036D to group ID 036A
Group ID 036E, members;add members 036F:0371 to group ID 036E
# 2  
Old 12-03-2010
You could do something like:
Code:
echo "036A" | awk '
{
  printf("Group ID %04X, members;add members %04X:%04X to group ID %04X\n", hex2dec($1) ,hex2dec($1)+1, hex2dec($1)+3, hex2dec($1))
  printf("Group ID %04X, members;add members %04X:%04X to group ID %04X\n", hex2dec($1)+4 ,hex2dec($1)+5, hex2dec($1)+7, hex2dec($1)+4)
}
function hex2dec(h,i,x,v){
  h=tolower(h);sub(/^0x/,"",h)
  for(i=1;i<=length(h);++i){
    x=index("0123456789abcdef",substr(h,i,1))
    v=(16*v)+x-1
  }
  return v
}'

# 3  
Old 12-03-2010
Can we input Starting HEX number = 036A, Number of groups = 2, Members in a Group= 4.
# 4  
Old 12-03-2010
Quote:
Originally Posted by dynamax
Can we input Starting HEX number = 036A, Number of groups = 2, Members in a Group= 4.
Code:
echo "036A 2 4" | awk '
{
  for(i=0;i<$2;i++) {
    printf("Group ID %04X, members;add members %04X:%04X to group ID %04X\n", hex2dec($1)+i*$3 ,hex2dec($1)+i*$3+1, hex2dec($1)+i*$3+3, hex2dec($1)+i*$3)
  }
}
function hex2dec(h,i,x,v){
  h=tolower(h);sub(/^0x/,"",h)
  for(i=1;i<=length(h);++i){
    x=index("0123456789abcdef",substr(h,i,1))
    v=(16*v)+x-1
  }
  return v
}'

# 5  
Old 12-17-2010
Code:
/export/home/e0007fc/work#sudo ./junk0
+ awk
{
  for(i=0;i<$2;i++) {
    printf("Group ID %04X, members;add members %04X:%04X to group ID %04X\n", hex2dec($1)+i*$3 ,hex2dec($1)+i*$3+1, hex2dec($1)+i*$3+3, hex2dec($1)+i*$3)
  }
}
function hex2dec(h,i,x,v){
  h=tolower(h);sub(/^0x/,"",h)
  for(i=1;i<=length(h);++i){
    x=index("0123456789abcdef",substr(h,i,1))
    v=(16*v)+x-1
  }
  return v
}
+ echo 036A 2 4
awk: syntax error near line 7
awk: bailing out near line 7


Last edited by Franklin52; 12-18-2010 at 08:54 AM.. Reason: Please use code tags
# 6  
Old 12-18-2010
Use nawk or /usr/xpg4/bin/awk on Solaris.
This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 (5 Replies)
Discussion started by: jocazh
5 Replies

2. Shell Programming and Scripting

Name grouping

awk 'FNR==NR {a; next} $NF in a' genes.txt refseq_exons.txt > output.txt I can not figure out how to group the same name in $4 together. Basically, all the SKI together in separate rows and all the TGFB2. Thank you :). chr1 2160133 2161174 SKI chr1 218518675 218520389 TGFB2... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

How to replace with "sed" some hex values by other hex values?

Assume I have a file \usr\home\\somedir\myfile123.txt and I want to replace all occurencies of the two (concatenated) hex values x'AD' x'A0' bytwo other (concatenated) hex values x'20' x'6E' How can I achieve this with the gnu sed tool? Additional question: Is there a way to let sed show... (1 Reply)
Discussion started by: pstein
1 Replies

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

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

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

What is the difference between ios::hex and std::hex?

Hi, Is there really a difference between these two, std::hex and ios::hex?? I stumbled upon reading a line, "std::ios::hex is a bitmask (8 on gcc) and works with setf(). std::hex is the operator". Is this true? Thanks (0 Replies)
Discussion started by: royalibrahim
0 Replies

8. Programming

After converting the hexstr to Hex and storing the Hex in a char*

Hi All, My main intension of is to convert the Hexstring stored in a char* into hex and then prefixing it with "0x" and suffix it with ',' This has to be done for all the hexstring char* is NULL. Store the result prefixed with "0x" and suffixed with ',' in another char* and pass it to... (1 Reply)
Discussion started by: rvan
1 Replies

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

10. Shell Programming and Scripting

grouping of objects and print their number

hello have one file N H H K L K K H K N so output shud be like N = 2 H = 3 K = 4 L =1 (3 Replies)
Discussion started by: cdfd123
3 Replies
Login or Register to Ask a Question