Hex number sequence help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hex number sequence help
# 8  
Old 09-12-2013
strtonum is painful to try and do without. Fortunately the makers of GAWK were nice enough to make a nawk version for you.

Code:
nawk 'function mystrtonum(str, ret, chars, n, i, k, c)
     {
         if (str ~ /^0[0-7]*$/) {
             # octal
             n = length(str)
             ret = 0
             for (i = 1; i <= n; i++) {
                 c = substr(str, i, 1)
                 if ((k = index("01234567", c)) > 0)
                     k-- # adjust for 1-basing in awk
     
                 ret = ret * 8 + k
             }
         } else if (str ~ /^0[xX][[:xdigit:]]+/) {
             # hexadecimal
             str = substr(str, 3)    # lop off leading 0x
             n = length(str)
             ret = 0
             for (i = 1; i <= n; i++) {
                 c = substr(str, i, 1)
                 c = tolower(c)
                 if ((k = index("0123456789", c)) > 0)
                     k-- # adjust for 1-basing in awk
                 else if ((k = index("abcdef", c)) > 0)
                     k += 9
     
                 ret = ret * 16 + k
             }
         } else if (str ~ \
       /^[-+]?([0-9]+([.][0-9]*([Ee][0-9]+)?)?|([.][0-9]+([Ee][-+]?[0-9]+)?))$/) {
             # decimal number, possibly floating point
             ret = str + 0
         } else
             ret = "NOT-A-NUMBER"
     
         return ret
     }

     ...
     restofthecode
     ...'

This User Gave Thanks to Corona688 For This Post:
# 9  
Old 09-12-2013
A portable solution (which is obviously less efficient):
Code:
f=input_filename

echo ibase=16 | cat - "$f" | bc | paste - "$f" | sed '1!p' | paste - - |
awk 'a=="" {a=$2} ($1+1 != $3) || NF == 2 {print a (a != $2 ? ":"$2 : ""); a=""}'

Tested with all your data samples and gives the correct result.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 10  
Old 09-12-2013
And a nawk version:

Code:
$ cat hex.awk

function mystrtonum(str, ret, chars, n, i, k, c)
     {
         if (str ~ /^0[0-7]*$/) {
             # octal
             n = length(str)
             ret = 0
             for (i = 1; i <= n; i++) {
                 c = substr(str, i, 1)
                 if ((k = index("01234567", c)) > 0)
                     k-- # adjust for 1-basing in awk

                 ret = ret * 8 + k
             }
         } else if (str ~ /^0[xX][[:xdigit:]]+/) {
             # hexadecimal
             str = substr(str, 3)    # lop off leading 0x
             n = length(str)
             ret = 0
             for (i = 1; i <= n; i++) {
                 c = substr(str, i, 1)
                 c = tolower(c)
                 if ((k = index("0123456789", c)) > 0)
                     k-- # adjust for 1-basing in awk
                 else if ((k = index("abcdef", c)) > 0)
                     k += 9

                 ret = ret * 16 + k
             }
         } else if (str ~ \
       /^[-+]?([0-9]+([.][0-9]*([Ee][0-9]+)?)?|([.][0-9]+([Ee][-+]?[0-9]+)?))$/) {
             # decimal number, possibly floating point
             ret = str + 0
         } else
             ret = "NOT-A-NUMBER"

         return ret
     }


P == "" {       P=strtonum("0x"$1); K=0;        next    }
(P+K+1) == strtonum("0x"$1) {   K++;    next    }

{
        if(!K)  printf("%04X\n", P);
        else    printf("%04X:%04X\n", P, P+K);
        P=strtonum("0x"$1);     K=0;
}

END {
        if(!K)  printf("%04X\n", P);
        else    printf("%04X:%04X\n", P, P+K);
}

$ nawk -f hex.awk data

0DDB:0DDF
0DE1
0DE4:0DE8

$

# 11  
Old 09-12-2013
Both work .... fine ...

Thanks a lot Guys !!
# 12  
Old 09-12-2013
No awk involved, but bash:
Code:
while read A
  do B=$((0x$A))
     [[ -z $L ]] && { printf "%04X" $B; L=B-1; }
     [[ $L+1 -eq $B ]] && K=0 || { [[ $K -eq 1 ]] && printf "\n%04X" $B || { printf ":%04X\n%04X" $L $B; K=1; }; }
     L=$B
  done < file
[[ $K -eq 0 ]] && printf ":%04X\n" $B || printf "\n"
0DF6:0DFD
0DFF:0E09
0E0C:0E10
0E14

0DE1:0DEC

0DDB:0DDF
0DE1
0DE4:0DE8

This User Gave Thanks to RudiC 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

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

3. Shell Programming and Scripting

How to take the missing sequence Number?

Am using unix aix KSH... I have the files called MMRR0106.DAT MMRR0206.DAT MMRR0406.DAT MMRR0506.DAT MMRR0806.DAT .... ... MMRR3006.DAT MMRR0207.DAT These files are in one dircetory /venky ? I want the output like this ? Missing files are : MMRR0306.DAT MMRR0606.DAT... (7 Replies)
Discussion started by: Venkatesh1
7 Replies

4. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 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. 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

8. Shell Programming and Scripting

Hex numbering sequence

Friends, 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 a group should have and total groups. e.g: Here we are generating 2 groups with 4 Members each starting with hex 036A. Output: Group ID 036A,... (5 Replies)
Discussion started by: dynamax
5 Replies

9. Shell Programming and Scripting

Changing the sequence number

Hi, I have a data as follow: 1 400 2 239 3 871 4 219 5 543 6 ... 7 ... .. ... .. ... 99 818 100 991 I want to replace the sequence number (column 1) that start from 150. The output should like this: 150 400 151 239 (3 Replies)
Discussion started by: nica
3 Replies

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