The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-31-2007
gauravgoel gauravgoel is offline
Registered User
  
 

Join Date: Dec 2005
Location: India
Posts: 218
AWK Help

HI All,

Need a help

Suppose I have a file having the data

1,FF,233

where the second field is Hex
now I want to convert this hex in dec and see the output as

1,255,233

Request you all to help

Regards,
Gaurav Goel
  #2 (permalink)  
Old 03-31-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,518
If you have Python, here's an alternative
Code:
for line in open("file"):
    line = line.strip().split(",")
    print "%s,%s,%s" % (line[0],str(int(line[1],16)), line[2])
otherwise, people have rolled out these functions in awk (i have not used them). you can have a try
Code:
function hex2dec(x,    h, i, n, l) {
    h = "0123456789ABCDEF..........abcdef"
    for (i = l = length(x); i > 0; i--)
        n += (index(h, substr(x, i, 1)) - 1) % 16 * 16 ^ (l - i)
    return n

}

function dec2hex(d) {
    return sprintf("%lx", d)

}
  #3 (permalink)  
Old 03-31-2007
ennstate ennstate is offline
Registered User
  
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Using printf

Using printf
HTML Code:
#/bin/ksh
for v in $(cut -d, -f2 /tmp/hex) ; do
   printf '%d\n' "0x$v"
done
Using bc
HTML Code:
#/bin/ksh
 for v in $(cut -d, -f2 /tmp/hex) ; do
   print "ibase=16;$v" |bc
 done
cat /tmp/hex
1,FF,233
1,EF,233

Output:
255
239

Hi Gurus,
Please let us know if the following awk can be used for the above conversion,
HTML Code:
 awk -F, ' { printf("The Decimal value of %s is [%x]\n", $2,$2) }'  /tmp/hex
I tried it but always gives me 0 for hexadecimal value.

Thanks
Nagarajan Ganesan
  #4 (permalink)  
Old 03-31-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
In ksh...
Code:
#! /usr/bin/ksh
typeset -i16 xf2
typeset -i10 df2
typeset -l f2
while IFS=, read f1 f2 f3 ; do
        xf2=16#$f2
        ((df2=xf2))
        echo  $f1,$df2,$f3
done
exit 0
  #5 (permalink)  
Old 04-02-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by ennstate
Please let us know if the following awk can be used for the above conversion,
HTML Code:
 awk -F, ' { printf("The Decimal value of %s is [%x]\n", $2,$2) }'  /tmp/hex
I tried it but always gives me 0 for hexadecimal value.
You can convert decimal to hexa and not the reverse using printf.
  #6 (permalink)  
Old 04-02-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
Quote:
Originally Posted by anbu23
You can convert decimal to hexa and not the reverse using printf.
Code:
awk -v VAR=A 'END{ printf"%s %s\"%s\n", "echo \"ibase=16;", VAR, " | bc" }' /dev/null | sh
Well, thats kind of possible!
  #7 (permalink)  
Old 04-02-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by matrixmadhan
Code:
awk -v VAR=A 'END{ printf"%s %s\"%s\n", "echo \"ibase=16;", VAR, " | bc" }' /dev/null | sh
Well, thats kind of possible!
What i meant was, its not possible using format specifiers provided in printf.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0