Print hex Ip address in decimal format inside awk script

Hi to all,

May someone help me with the following.

The awk script below is part of a bigger awk script, and this part attempts to print an Ip address that is in hex format in decimal format. I'm trying with following code but
I'm getting 0.0.0.0 and the correct answer is 192.168.140.100

echo "d djjd:C0 A8 8C 64 00 00 00" | 
awk '{a=gensub(/.+:(..) (..) (..) (..).+/,"\\1 \\2 \\3 ,"0x"b[2],"0x"b[3],"0x"b"]\\4","g");split(a,b);printf("%d.%d.%d.%d","0x"b[1],"0x"b[2],"0x"b[3],"0x"b[4])}'
0.0.0.0

Thanks in advance,

Hi, see NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it Post: 302618249
Regards.

Why that complicated? Try

echo "d djjd:C0 A8 8C 64 00 00 00" |  awk '{sub (/:/," "); printf("%d.%d.%d.%d\n","0x"$3,"0x"$4,"0x"$5,"0x"$6)}'
192.168.140.100

---------- Post updated at 17:46 ---------- Previous update was at 17:39 ----------

Or

echo "d djjd:C0 A8 8C 64 00 00 00" |  awk '{X="0x"; sub (/:/," "); printf("%d.%d.%d.%d\n", X$3, X$4, X$5, X$6)}'
192.168.140.100

Thanks disedorgue/RudiC.

I've tried both solutions of RudiC and with both I get 0.0.0.0. I've tried in GNU/Linux, Cygwin and CentOS 6.7.

I'm not sure why.

Thanks in advance

Post your attempts.

When I try with your solutions there is no more as output like 0.0.0.0

When I try with my script I get the same 0.0.0.0.

I've done the following tests and it works when don't go the commas and work with single comma and without comma. It seems the issue begins when I try to convert variables that contain hex values.

$ echo "d djjd:C0 A8 8C 64 00 00 00" | awk '{sub (/:/," "); printf("%d", 0xC0)}'
192
$ echo "d djjd:C0 A8 8C 64 00 00 00" | awk '{sub (/:/," "); printf("%d", '0xC0')}'
192
$ echo "d djjd:C0 A8 8C 64 00 00 00" | awk '{sub (/:/," "); printf("%d", "0xC0")}'
0
$ awk 'BEGIN{Z=C0; printf("%d", 0xZ)}'
0

Above is diametrically opposed to what I get:

echo "d djjd:C0 A8 8C 64 00 00 00" | awk '{sub (/:/," "); printf("%d", 0xC0)}'
0
echo "d djjd:C0 A8 8C 64 00 00 00" | awk '{sub (/:/," "); printf("%d", '0xC0')}'
0
echo "d djjd:C0 A8 8C 64 00 00 00" | awk '{sub (/:/," "); printf("%d", "0xC0")}'
192

The variables or constants need to be string "0x" plus a hex value string, e.g. "C0". That why your fourth attempt doesn't work - Z is assigned the number 0.

With double commas don't work when asigning a value to variable.
And if a variable has a hex value and I try to concatenate the value with "0x" and store in a 2nd variable, only prints "0x".

$ awk 'BEGIN{Z=0xC0; printf("%d", Z)}'
192
$ awk 'BEGIN{Z='0xC0'; printf("%d", Z)}'
192 
$ awk 'BEGIN{Z="0xC0"; printf("%d", Z)}'
0
awk 'BEGIN{W=C0;Z="0x"W; printf("%d", Z)}'
0x
$ awk 'BEGIN{W="C0";Z="0x"W; printf("%s", Z)}'
0xC0
$ awk 'BEGIN{W="C0";Z="0x"W; printf("%d", Z)}'
0

---------- Post updated at 03:28 PM ---------- Previous update was at 03:00 PM ----------

Thanks RudiC for help and time.

Like mentioned in this post, I tried adding "non decimal data" in awk command and only in that way works for me.

$ echo "d djjd:C0 A8 8C 64 00 00 00" | awk --non-decimal-data '{X="0x"; sub (/:/," "); printf("%d.%d.%d.%d\n", X$3, X$4, X$5, X$6)}'
192.168.140.100

Regards

Awk does not have consistent support for hexadecimal values. With gawk try the --non-decimal-data option (or try something like Corona688 posted in the link in post #2)

1 Like

Thanks Scrutinizer, exactly like I found in other thread I added non decimal data like you said and actually works.

Thanks for your help too!