Sponsored Content
Top Forums Shell Programming and Scripting NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it Post 302618185 by PCGameGuy on Tuesday 3rd of April 2012 05:31:55 PM
Old 04-03-2012
NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there.

I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and append 0x to the front but the printf statement seems not to work to convert it to decimal. I dumped my output to a file and have been trying to get it to convert so I can proceed on.

Here are the last lines snipped from my file, as it it large:


Code:
0x0C0,###0,33,6291
0x011,###1,59,0
0x009,###0,59,362147
0x011,###2,59,260
0x00E,###2,59,1051
0x008,###1,33,0
0x034,###2,59,0
0x001,###3,59,208
0x00E,###2,33,52
0x034,###2,33,0
0x00F,###1,59,0
0x009,###0,33,33022

From the shell prompt I tried:

Code:
printf  "%d\n" 0x03E

and get the proper output:

62

If I type:

Code:
cat <file>|nawk -F, '{printf "%d\n" , $1}'

I get nothing but zeroes as output:


Code:
0
0
0
0
0
0
0
0
0


I tested via:

Code:
cat <file>|nawk -F, '{$1,"     ""%d\n",$1}'

and my output has the correct output from $1, but "%d\n" won't output the decimal equivalent:


Code:
0x0C0     0
0x011     0
0x009     0
0x011     0
0x00E     0
0x008     0
0x034     0
0x001     0
0x00E     0
0x034     0
0x00F     0
0x009     0



So what am I missing? I have had it suggested that printf is different in awk than in the shell but that seemed wrong from what I read.

Last edited by PCGameGuy; 04-03-2012 at 07:20 PM.. Reason: Edited for clarity
 

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Decimal to Hexadecimal conversion

Hi frnds :) I need a small help... I have a very long file containing 20 digits decimal number which i want to convert into the corresponding 16 digit hexadecimal values. File looks like.... 11908486672755551741 05446378739602232559 04862605079740156652 . . . I tried the script for i... (7 Replies)
Discussion started by: vanand420
7 Replies

3. UNIX for Dummies Questions & Answers

Hexadecimal to Decimal

Hi all, I have a small script to convert my HexaDecimal Input to Decimal as output. #!/bin/ksh hd=00208060 dec=`printf %d $hd` echo $dec Output of the above program: printf: 00208060 not completely converted 16 But my expected output is "2130016". How can i acheive this. I... (2 Replies)
Discussion started by: Arunprasad
2 Replies

4. Shell Programming and Scripting

[bash]printf octal instead of decimal

Hello everybody, I would like to understand why the printf function is returning me an octal value with this command : printf %4.4d 0010 returns 0008 printf %4.4d 10 returns 0010 Thanks for help. (3 Replies)
Discussion started by: dolphin06
3 Replies

5. Shell Programming and Scripting

printf Hexadecimal output

printf "%X\n" "A" 41 printf "%X\n" "2" 2 Expected 32 (not 2). Is there a "printf" which will output the hexadecimal value of a numeric character? (9 Replies)
Discussion started by: methyl
9 Replies

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

7. Shell Programming and Scripting

Printf question: getting padded zero in decimal plus floating point together.

Hi Experts, Quick question: I am trying to get the output with decimal and floating point but not working: echo "20.03" | awk '{printf "%03d.2f\n" , $0 }' 020.2f How to get the output as : 020.03 Thank you. (4 Replies)
Discussion started by: rveri
4 Replies

8. Shell Programming and Scripting

Hexadecimal to Binary conversion

Hi Guys, Is it possible to convert the hexadecimal to Binary by unix command.....I could not figure out.... If I need to convert AF6D to binary...what could be the way to do? Thanks in advance!! ---------- Post updated at 02:57 AM ---------- Previous update was at 02:42 AM ---------- I... (6 Replies)
Discussion started by: Indra2011
6 Replies

9. Shell Programming and Scripting

Conversion from Hexadecimal to binary

How can I convert hexadecimal values to Binary from the second field to the end Input: WS-2 23 345 235 DT-3 45 4A3 000 pp-2 76 300 E4 Output: WS-2 100011 1101000101 1000110101 DT-3 1000101 10010100011 000 pp-2 1110110 1100000000 11100100 (16 Replies)
Discussion started by: aydj
16 Replies

10. 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
ppmcolormask(1) 					      General Commands Manual						   ppmcolormask(1)

NAME
ppmcolormask - produce mask of areas of a certain color in a PPM file SYNOPSIS
ppmcolormask color [ppmfile] DESCRIPTION
Reads a PPM file as input. Produces a PBM (bitmap) file as output. The output file is the same dimensions as the input file and is black in all places where the input file is the color color, and white everywhere else. The output of ppmcolormask is useful as an alpha mask input to pnmcomp. Note that you can generate such an alpha mask automatically as you convert to PNG format with pnmtopng(1). Use its -transparent option. ppmfile is the input file. If you don't specify ppmfile, the input is from Standard Input. The output goes to Standard Output. You can specify color five ways: o An X11-style color name (e.g. black). o An X11-style hexadecimal specifier: rgb:r/g/b, where r g and b are each 1- to 4-digit hexadecimal numbers. o An X11-style decimal specifier: rgbi:r/g/b, where r g and b are floating point numbers between 0 and 1. o For backwards compatibility, an old-X11-style hexadecimal number: #rgb, #rrggbb, #rrrgggbbb, or #rrrrggggbbbb. o For backwards compatibility, a triplet of numbers separated by commas: r,g,b, where r g and b are floating point numbers between 0 and 1. (This style was added before MIT came up with the similar rgbi style.) SEE ALSO
pgmtoppm(1), pnmcomp(1), pbmmask(1), ppm(5) AUTHOR
Bryan Henderson (bryanh@giraffe-data.com) 14 April 2000 ppmcolormask(1)
All times are GMT -4. The time now is 02:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy