Register
It is currently Fri Oct 24, 2014 8:42 am

hex headache


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Jun 28, 2010 9:39 pm   

Joined: Thu Oct 09, 2008 3:26 am
Posts: 15
Location: Columbus, OH
I've tried several shots at this and I'm having trouble,
file format:
MOT,32B,1,232E
$2 and $4 are hex and I need to convert to decimal. I've tried:
Code:
awk 'BEGIN {FS = ","}; {print $1, "0x"$2, $3, "0x"$4}; {printf "%s, %d, %s, %d\n"}'

The command line complains about the first printf string format in the third operation as being a problem. I've also tried playing with the --non-decimal-data, and $4 + 0 operations but this example is the farthest that the code gets to succcess before the command line complains:(partial)
FNR=1 not enough arguments to satisfy format string
(with the caret pointing to the first %s) and the message "ran out for this one".

Any ideas?
Thx!


Top
 Profile  
 PostPosted: Mon Jun 28, 2010 10:31 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
gimp25 wrote:
MOT,32B,1,232E
$2 and $4 are hex and I need to convert to decimal. I've tried:
Code:
awk 'BEGIN {FS = ","}; {print $1, "0x"$2, $3, "0x"$4}; {printf "%s, %d, %s, %d\n"}'

The command line complains about the first printf string format in the third operation as being a problem.


printf() uses the format printf("<format>", <value>);
your printf function has all the format tags, but no value tags, that's why it's running our of arguments.
Why would you print everything twice anyway? (print and printf?)
If you have GNU awk, there is a strtonum() function available to convert strings to numbers.
I think the following should help you out:
Code:
gawk -F, '{ printf("%s, %d, %s, %d\n", $1, strtonum("0x"$2), $3, strtonum("0x"$4)); }'


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP