Convert Hexdecimal to Decimal in Solaris awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Hexdecimal to Decimal in Solaris awk
# 1  
Old 05-08-2018
Convert Hexdecimal to Decimal in Solaris awk

Hi there,

i want to use this Linux-script on a Solaris System to check the fragmentation Level of ZFS-DataSets:

Code:
#!/bin/sh

zdb -ddddd ${1} | awk --non-decimal-data \
'
/Indirect blocks/ {
  file_number++;
  next_block = 0;
}

/L0/ {
  split($3, fields, ":");
  this_block = ("0x"fields[2])+0;
  this_block_size = ("0x"fields[3])+0;
  total_blocks++;
  if( next_block != 0 ) {
    if( next_block == this_block ) {
      not_fragmented++;
    }
    else {
      fragmented++;
    }
  }
  next_block =  this_block + this_block_size;
}

END {
  total_fragment_blocks = fragmented + not_fragmented;
  printf("There are %d files.\n", file_number );
  printf("There are %d blocks and %d fragment blocks.\n", total_blocks, total_fragment_blocks );
  printf("There are %d fragmented blocks (%2.2f%%).\n", fragmented, fragmented*100.0/total_fragment_blocks );
  printf("There are %d contiguous blocks (%2.2f%%).\n", not_fragmented, not_fragmented*100.0/total_fragment_blocks );





Everything seems to work fine except the conversion from hex to dec at the following lines:

Code:
this_block = ("0x"fields[2])+0;
 this_block_size = ("0x"fields[3])+0;

I realized, that this is can't be done so easely with Solaris AWK. I found an other NAWK Script on the Internet which can do this but i have no idea how implement this in the main script.

Here is the conversion script:
Code:
BEGIN    { for(i=0;i<10;i++)  decv[i]=i;
                   decv["a"]=10;decv["b"]=11;decv["c"]=12;decv["d"]=13;
                   decv["e"]=14;decv["f"]=15;
                   decv["A"]=10;decv["B"]=11;decv["C"]=12;decv["D"]=13;
                   decv["E"]=14;decv["F"]=15
                  }
        function hex(x) {
                  value = 0;
                  i     = 1;
                  n=length(x);
                  while ( n > 0 )  {
                        value = 16*value + decv[substr(x,i,1)];
                        n--;
                        i++;
                  }
                  return value
        }
               {printf("%12.0f\n",hex($1))}


Would be great if someone can help me.

Bye
Michael


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 05-08-2018 at 08:10 AM.. Reason: Added CODE tags, removed color abundance.
# 2  
Old 05-08-2018
Welcome to the forum.

It always helps if people in here see WHAT and HOW is going wrong. So, the input to the two questionable lines as well as the resulting output / value and / or error messages would be valuable. On first sight, with my awk version (mawk 1.3.3 Nov 1996), the conversion is done correctly.

Quote:
Originally Posted by Don Cragun
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
# 3  
Old 05-08-2018
Hi, you're absolut right, sorry for forgetting.
There were no special error messages except this:
Code:
bash-3.2# /script/fragmentation.sh tempool/data
There are 788 files.
There are 86526 blocks and 0 fragment blocks.
awk: division by zero
 record number 105858
bash-3.2#

what should be ok when the Pool is not fragmented.
Because this script never found any fragmention even on filesystems which are fragmented for sure i checked the awk statement line by line. To check the conversion of the two vars
Code:
this_block / this_block_size

is just added a printf stated to below them:
Code:
/L0/ {
  split($3, fields, ":");
  this_block = ("0x"fields[2])+0;
  this_block_size = ("0x"fields[3])+0;
  total_blocks++;

printf ("This Blocksize: " this_block "   This Blocksize Size: " this_block_size "\n" )

The result of this printf Statement ist always zero
Code:
This Blocksize: 0   This Blocksize Size: 0

what is not possible because the input line Shows
Code:
L0 0:26546fa00:800 4000L/800P F=10 B=308653/308653

so the result should be the decimal output of the HEX number 26546fa00 and the decimal output of the sum of 26546fa00 and 800

So i removed the "+0" in the Statement
Code:
this_block = ("0x"fields[2])+0;

and then there were numbers... but only those hexadecimal numbers without letters. Next i investigated the net and found several Posts with the same Problem on Solaris awk. The solution is to use this extra function described above but i don't have any idea how to implement this in this awk-oneliner.

The xpg4-awk and nawk show the same Problem with
Code:
 this_block = ("0x"fields[2])+0;
  this_block_size = ("0x"fields[3])+0;

plus it can't use "--non-decimal-data"
# 4  
Old 05-08-2018
The split($3, fields, ":"); with your input line L0 0:26546fa00:800 4000L/800P F=10 B=308653/308653 should have 4000L/800P in fields[1] and leave fields[2] and [3] empty... which explains the printf result perfectly.

EDIT: If I use above and do it for $2, it prints perfectly reasonable numbers:
Code:
10289084928
2048

# 5  
Old 05-08-2018
Unfortunately this is not the solution.

Here is a short sample of the problem:
Code:
echo "2658a5600:800" | awk '{split($0,fields,":"); firstHex2Dec = ("0x"fields[1])+0; secondHex2Dec = ("0x"fields[2])+0; \
printf ("firstHex2Dec: "firstHex2Dec); printf ("   secondHex2Dec: "secondHex2Dec "\n") }'
firstHex2Dec: 0   secondHex2Dec: 0

The output of firstHex2Dec und secondHex2Dec should be decimal 10293499392 and 2048 now but it is "0" because the "+0" seemes to overwrite the var.
Now i remove the "+0"
Code:
bash-3.2# echo "2658a5600:800" | awk '{split($0,fields,":"); firstHex2Dec = ("0x"fields[1]); secondHex2Dec = ("0x"fields[2]); \
printf ("firstHex2Dec: "firstHex2Dec); printf ("   secondHex2Dec: "secondHex2Dec "\n") }'
firstHex2Dec: 0x2658a5600   secondHex2Dec: 0x800

The content of firstHex2Dec and secondHex2Dec is not overwritten by the "+0" but it is still not the decimal value as expected.

Im pretty sure the statement ("0x"array[n])+0 doesn't work in Solaris.

My Version of AWK ist:

Code:
bash-3.2# pkginfo -l  SUNWesu
   PKGINST:  SUNWesu
      NAME:  Extended System Utilities
  CATEGORY:  system
      ARCH:  i386
   VERSION:  11.10.0,REV=2005.01.21.16.34
   BASEDIR:  /
    VENDOR:  Oracle Corporation
      DESC:  additional UNIX system utilities, including awk, bc, cal, compress, diff, dos2unix, last, rup, sort, spell, sum, uniq, and uuencode
    PSTAMP:  on10ptchfeatx20130110213921
  INSTDATE:  Jan 17 2018 15:56
   HOTLINE:  Please contact your local service provider
    STATUS:  completely installed
     FILES:      169 installed pathnames
                  12 shared pathnames
                  21 linked files
                  18 directories
                 107 executables
                3726 blocks used (approx)

Thank you in advance
# 6  
Old 05-08-2018
OK. Does it provide the sprintf function? Try
Code:
echo "2658a5600:800" | awk '{split($0,fields,":"); firstHex2Dec = sprintf ("%.f", "0x"fields[1]); secondHex2Dec = sprintf ("%.f", "0x"fields[2]);\
printf ("firstHex2Dec: "firstHex2Dec); printf ("   secondHex2Dec: "secondHex2Dec "\n") }'
firstHex2Dec: 10293499392   secondHex2Dec: 2048

# 7  
Old 05-08-2018
Hi,

here is the output:

Code:
bash-3.2# echo "2658a5600:800" | awk '{split($0,fields,":"); firstHex2Dec = sprintf ("%.f", "0x"fields[1]); secondHex2Dec = sprintf ("%.f", "0x"fields[2]);\
printf ("firstHex2Dec: "firstHex2Dec); printf ("   secondHex2Dec: "secondHex2Dec "\n") }'
firstHex2Dec: 0   secondHex2Dec: 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. UNIX for Dummies Questions & Answers

Convert hexadecimal value to decimal value

Hi All, cat filename | awk '{print $1, $2, $4, $5, $6, $7, $8, $9, $10;}' | awk 'NF > 0' OUTPUT: 2015-01-19 00:12:32 00000000fbfa0000 000000009ae5cf80 014d 015d 0017 003c 0362de20 2015-01-19 00:13:52 00000000fc820000 00000000994c6758 014c 015d 000b 003c 08670250 2015-01-19 00:14:25... (12 Replies)
Discussion started by: sam@sam
12 Replies

3. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

4. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

5. Shell Programming and Scripting

Convert hexadecimal value in decimal value

hi all, this is my script: #! /bin/sh minutes=$( { i2cget -f -y 0 0x51 3; } 2>&1 ) minutes=${minutes:2} hour=$( { i2cget -f -y 0 0x51 4; } 2>&1 ) hour=${hour:2} day=$( { i2cget -f -y 0 0x51 5; } 2>&1 ) day=${day:2} month=$( { i2cget -f -y 0 0x51 7; } 2>&1 ) month=${month:2} ... (6 Replies)
Discussion started by: enaud
6 Replies

6. Shell Programming and Scripting

Convert hex to decimal

can someone help me in converting hex streams to decimal values using perl script Hex value: $my_hex_stream="0c07ac14001676"; Every hex value in the above stream should be converted in to decimal and separated by comma. The output should be: 12,07,172,20,00,22,118 (2 Replies)
Discussion started by: Arun_Linux
2 Replies

7. Shell Programming and Scripting

need to convert a decimal value to an ascii char with awk for the field delimiter

Hello, I need an awk script to receive a variable that's an decimal value such as 009 or 031 and then convert this value to an ascii character to use as the FS (field separator for the input file). For example, 009 should be converted to an ascii tab 031 should be converted to an ascii... (1 Reply)
Discussion started by: script_op2a
1 Replies

8. Shell Programming and Scripting

Convert hex to decimal or reverse is better?

Please Help Me! about the problem down under. I have 2 files with nearly the same characteristics, I have to convert one to the other format or the other format to one's format. I want to write it with awk. The first file contain lines like this: 300000001#A#Y#Y#Y#Y The other file contain... (4 Replies)
Discussion started by: Axel82
4 Replies

9. Shell Programming and Scripting

Convert exponential value to decimal

Hi, Here is my script to read a file into array: awk -F '+' ' # load first file into array indexed by fields 1 and 2 NR == FNR { file1nr = FNR for (i=3; i<NF; i++) { file1 = $i } I have this... (5 Replies)
Discussion started by: Sangtha
5 Replies

10. UNIX for Dummies Questions & Answers

Convert hexadecimal to decimal base

Hello ! Does anyone knows how can I convert hexadecimal to decimal base in the ksh or csh script ?? Thanks ! Witt (1 Reply)
Discussion started by: witt
1 Replies
Login or Register to Ask a Question