Solved

AWK - how to compare 2 HEX number ?

Posted on 2001-06-12
8
662 Views
Last Modified: 2007-12-19
I wish to compare last 4 digit of 2 hex numbers using AWK, how can I achieve this?

$5 = 123456AB
$4 = 1234ABCD

if( substr($5,5,4) >= substr($4,5,4) )      
 {                                                      
 printf("Greater\n");  
 }                                                      
 else                                                  
 {                                                      
 printf("Greater\n");  
 }          

Thanks.                                            
                                                       
}                                                      
0
Comment
Question by:qiang8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:mrn060900
ID: 6180685
Maybe It would be easier to do if you converted the hex string to dec?

have a look at this script

# The script reads input from a datafile, and
# prints the result on standard output#
echo $1 | awk '{   # Redirect arg#1(filename) to "awk"filename=$1

# Validate that argument is passed and file exists.if ( NF != 1 ) {
        print "Usage: conv filename"        exit        }
if ( (getline < filename) == -1 ) {
        print "Cannot open the File:", filename        exit        }

# do --- while loop to read all the records from filedo {
# Binary to Decimal Conversion##################################

if ( $1 ~ /\<[bB]:[dD]\>/ ) {  # Check the option from file
        if ( $2 ~ /[^01]/ ) {  # Make sure data is Binary
                print "Corrupt Data:", $0                exit                }
        # Convert Binary to Decimal        digitCount=length($2);
        numDec=0        for(i=0; i < digitCount; i++)
                numDec=numDec + ( substr($2,digitCount - i,1) + 0) * 2^i
        print "Binary:", $2, "=", "Decimal:", numDec        }

# Binary to Octal Conversion##############################

else if ( $1 ~ /\<[bB]:[oO]\>/ ) { # Check the option from file
        if ( $2 ~ /[^01]/ ) { # Make sure data is Binary
                print "Corrupt Data:", $0                exit                }
        # Convert Binary to octal        digitCount=length($2);
        sNumOct=""        for(i=0; i < digitCount; i+=3) {
                numOct=0                if ( (digitCount - i) < 3)
                        digits = digitCount - i                else
                        digits = 3                for( j=0; j < digits; j++)
                        numOct=numOct + (substr($2,digitCount-i-j,1)+0) *2^j
                sNumOct = numOct sNumOct                }
        print "Binary:", $2, "=", "Octal :", sNumOct        }

# Octal to Binary Coversion#############################

else if ( $1 ~ /\<[oO]:[bB]\>/ ) {
        if ( $2 ~ /[^0-7]/ ) { # Make sure data is Octal
                print "Corrupt Data:", $0                exit                }
        digitCount=length($2)        sNumBin=""
        for(i=0; i < digitCount; i++) {
                curDigit=substr($2,digitCount-i,1)
                for(j=0; j < 3; j++) {                        numBin=curDigit%2
                        curDigit=(curDigit - numBin)/2
                        sNumBin=numBin sNumBin
                                      }                                        }
        print "Octal:", $2, "=", "Binary:", sNumBin        }

# Decimal to Binary Conversion###############################

else if ( $1 ~ /\<[dD]:[bB]\>/ ) {        if ( $2 ~ /[^0-9]/ ) {
                print "Corrupt Data:", $0                exit                }
        sNumBin=""        numDec=$2        while(numDec != 0) {
                numBin=numDec%2                numDec=(numDec - numBin)/2
                sNumBin=numBin sNumBin                }
        print "Decimal:", $2, "=", "Binary:", sNumBin        }

# Hex to Binary Conversion############################

else if ( $1 ~ /\<[hH]:[bB]\>/ ) {        if ( $2 ~ /[^0-9a-fA-f]/ ) {
                print "Corrupt Data:", $0                exit                }
        numHex=$2        digitCount=length($2)        sNumBin=""
        for(i=0; i < digitCount; i++) {
                curDigit=substr($2,digitCount-i,1)
                # Used tr command to change letters A-F to numbers
                ##################################################
                if(curDigit  ~ /[a-fA-f]/) {
                        trCom="echo " curDigit "| tr \"A-Fa-f\" \"0-50-5\""
                        trCom | getline                        curDigit=$1 + 10
                        close(trCom)                        }
                for(j=0; j < 4; j++) {                        numBin=curDigit%2
                        curDigit=(curDigit - numBin)/2
                        sNumBin=numBin sNumBin
                                      }                                        }
        print "Hex:", numHex, "=", "Binary:", sNumBin
                                }# Binary to Hex Conversion
#############################else if ( $1 ~ /\<[bB]:[hH]\>/ ) {
        if ( $2 ~ /[^01]/ ) {                print "Corrupt Data:", $0
                exit                }        digitCount=length($2);
        sNumBin=$2        sNumHex=""        for(i=0; i < digitCount; i+=4) {
                numHex=0                if ( (digitCount - i) < 4)
                        digits = digitCount - i                else
                        digits = 4                for( j=0; j < digits; j++)
                        numHex=numHex+(substr(sNumBin,digitCount-i-j,1)+0) *2^j
                # Used tr command to change numbers to letters
                ##################################################
                if(numHex > 9) {                        numHex=numHex - 10
                        trCom="echo " numHex "| tr \"0-5\" \"A-F\""
                        trCom | getline                        numHex=$1
                        close(trCom)                        }
                sNumHex = numHex sNumHex                }
        print "Binary:", sNumBin, "=", "Hex :", sNumHex        }
# Message for the features not implemented
##########################################
else    print "Option", $1, "not implemented"
 } while ((getline < filename) != 0)}'


There are a couple more utils about hex2dec and dec2hex that may be useful.

00 nul  10 dle  20 sp   30 0   40 @   50 P    60 `   70 p
01 soh  11 dc1  21 !    31 1   41 A   51 Q    61 a   71 q
02 stx  12 dc2  22 "    32 2   42 B   52 R    62 b   72 r 03 etx  13 dc3  23 #    33 3   43 C   53 S    63 c   73 s 04 eot  14 dc4  24 $    34 4   44 D   54 T    64 d   74 t 05 enq  15 nak  25 %    35 5   45 E   55 U    65 e   75 u 06 ack  16 syn  26 &    36 6   46 F   56 V    66 f   76 v
07 bel  17 etb  27 '    37 7   47 G   57 W    67 g   77 w 08 bs   18 can  28 (    38 8   48 H   58 X    68 h   78 x
09 ht   19 em   29 )    39 9   49 I   59 Y    69 i   79 y
0a nl   1a sub  2a *    3a :   4a J   5a Z    6a j   7a y
0b vt   1b esc  2b +    3b ;   4b K   5b [    6b k   7b {
0c np   1c fs   2c ,    3c <   4c L   5c \    6c l   7c |
0d cr   1d gs   2d -    3d =   4d M   5d ]    6d m   7d }
0e so   1e rs   2e .    3e >   4e N   5e ^    6e n   7e ~
0f si   1f us   2f /    3f ?   4f O   5f _    6f o   7f del

regards Mike
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6181091
You can also do this pretty easily in bc:

  $ bc
  ibase=16
  123456AB - 1234ABCD
  -21794

It would be pretty easy to wrap this in some shell code that does what you want.
0
 
LVL 3

Expert Comment

by:mrn060900
ID: 6181816
Why didn't I think of that :-( . The best solutions are always the easiest...

Mike
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:qiang8
ID: 6184958
Chris,

Can u show me how to wrap this up in a shell code?? No idea at this moment. Your help is appreciated. Thanks.
0
 

Author Comment

by:qiang8
ID: 6185002
I mean read input from a file, perform the compare function and output the result to another file say out.txt.

The input file:

U10 5310B4A7 5310B4A3
U11 540E983D 540E983B
U06 551259BE 551259BA
U05 56C09056 56C09052    
U01 5711A3D6 5711A3D6
U02 5811EB6E 5811EB6E

Out.txt - insert *G* into 4th column if $3 > $2

U10 5310B4A7 5310B4A3
U11 540E983D 540E983B
U06 551259BE 551259BA
U05 56C09056 56C09052    
U01 5711A3D6 5711A3D7 *G*
U02 5811EB6E 5811EB6F *G*

Thanks again.



0
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 100 total points
ID: 6185972
#!/usr/bin/ksh -
#
# Read three columns and create a fourth that has a "G" if
# col1 > col2 in hex
#
# Copyright (c) 2001 by Chris Calabrese
# (chris_calabrese@yahoo.com).
# The author grants permission to use/modify this program in
# any way as long as:
#   1. It is not resold for profit.
#   2. This copyright notice accompanies any distribution.
# All other rights reserved.  Finally, the author makes no warantee of
# correctness or fitness for any purpose.

#
# Start up bc as a co-process
# We use a co-process here so that we don't incur the
# overhead of starting up bc for every input line.
#
bc |&

#
# Tell bc that the input values are in hex
#
print -p 'ibase=16'

#
# Read the input, get bc to do the real work,
# and generate the output
#
while read uvalue hex1 hex2
do  print -p "$hex1 - $hex2"
    read -p diff
    if [ "$diff" -lt 0 ]
    then    flag="G"
    else    flag=""
    fi
    print -r -- "$uvalue $hex1 $hex2 $flag"
done
0
 

Author Comment

by:qiang8
ID: 6189220
Thanks. I understand better now on how to create a co-process within a shell script as I'm pretty new to UNIX.
0
 

Author Comment

by:qiang8
ID: 6189222
Thanks a lot.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question