LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2010, 07:03 AM   #1
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Rep: Reputation: 0
Convert hex to decimal inside script with awk


All,

I have a script that creates a textfile with ip addresses and ports, but the ports are in hex format so they need to be converted.
Currently I have this code:

Code:
myscript.sh 10.10.10.10 | awk '{print $$2,$6,$4}'

10.11.12.13 06DF 10.13.18.20
10.10.10.11 0600 10.14.15.16
10.12.13.14 06DF 10.16.17.81
I'd like to have the second column in decimal instead of hex, like this:

Code:
10.11.12.13 1759 10.13.18.20
10.10.10.11 1536 10.14.15.16
10.12.13.14 1759 10.16.17.81
Is there any way to do that with awk, preferably in a oneliner? Would printf be an option?

Thanks!
 
Old 12-14-2010, 07:19 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
If you want to convert in myscript.sh then http://tldp.org/LDP/abs/html/mathc.html#HEXCONVERT shows a technique. bc is another option. If you want to convert in the awk then prefix the second column string with 0x or 0X and use the strtonum function on it.
 
Old 12-14-2010, 07:45 AM   #3
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
If you want to convert in myscript.sh then http://tldp.org/LDP/abs/html/mathc.html#HEXCONVERT shows a technique. bc is another option. If you want to convert in the awk then prefix the second column string with 0x or 0X and use the strtonum function on it.
I am not very familiar with functions and string operations i awk, but this gives an error:
myscript.sh 10.10.10.10 | awk '{print $2,strtonum(0x$6),$4}'
awk: calling undefined function strtonum

Any clue?
 
Old 12-14-2010, 08:06 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Which awk version are you using? Works here:
Code:
c@CW8:~$ echo | awk '{print strtonum( 0xa )}'
10
c@CW8:~$ awk --version
GNU Awk 3.1.8
[snip]
 
Old 12-14-2010, 08:35 AM   #5
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Which awk version are you using?
Mine states awk version 20050424 (FreeBSD) so I guess that function was not available at that time.
Rather than upgrading awk (I have quite some scripts relying on it so I don't want to break anything) I tried to use gawk (v3.1.5):

Code:
./myscript.sh 10.10.10.10 | gawk '{print $2,$7,$4}'
10.11.12.13 08BA  10.12.14.16
13.14.15.16 07FA 10.2.3.4
10.20.30.40 01BD  10.15.16.17

./myscript.sh 10.10.10.10 | gawk '{print $2,strtonum( 0x$7 ),$4}'
10.11.12.13 8 10.12.14.16
13.14.15.16 7 10.2.3.4
10.20.30.40 1 10.15.16.17
It recognizes the function but the conversion is wrong... 08BA is 2234 and not 8
 
Old 12-14-2010, 08:45 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
0x$7 does not concatenate string 0x onto the value of $7 (I do not know awk well enough to know what it does do). You need to make 0x a string; the default operation is concatenation:
Code:
c@CW8:~$ echo 08BA | awk '{print strtonum( "0x" $1 )}'
2234
 
Old 12-14-2010, 08:50 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017

Rep: Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196
Well personally I think you should be looking inside your myscript and doing the conversion there, however, try:
Code:
gawk '{print $2,strtonum( "0x"$7 ),$4}'
 
Old 12-14-2010, 09:16 AM   #8
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
0x$7 does not concatenate string 0x onto the value of $7 (I do not know awk well enough to know what it does do). You need to make 0x a string; the default operation is concatenation:
Code:
c@CW8:~$ echo 08BA | awk '{print strtonum( "0x" $1 )}'
2234

That did the trick... Many thanks for the help, appreciated!
 
Old 12-14-2010, 09:18 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Ah grail How you doing? Good you've dropped in. What does 0x$7 become that strtonum converts it to 8?
 
Old 12-14-2010, 09:53 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017

Rep: Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196
Quote:
What does 0x$7 become that strtonum converts it to 8?
The thing to remember about strtonum is that it ignores all strings that are not numbers as well as the fact that all variables are auto set to 0 when used.
So
Code:
0 = 0
x = 0
$7 = 08BA

strtonum(00"08BA")

# BA is ignored
strtonum(00"08")

8
 
1 members found this post helpful.
Old 12-14-2010, 12:06 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks grail It's so simple now you have explained it.
 
Old 12-14-2010, 08:19 PM   #12
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
One-liner requested; Perl to the rescue.
Code:
perl -e 'while($rec=<>){@fields=split /\s+/,$rec; printf( "%s %d %s\n", $fields[0],hex($fields[1]),$fields[2]);}' filename.dat
Of course, you could make it into a proper, readable, script...

--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How Can I convert Hex to Binary in a perl script telecom_is_me Programming 18 10-11-2010 04:34 PM
Decimal to Binary to Hex howto Gortex Linux - Newbie 7 03-05-2009 03:29 PM
Nice script to convert numbers between bases (bin/dec/oct/hex) hgate73 Linux - General 4 02-17-2009 03:51 AM
Is it possible to manipulate strings INSIDE an awk script? Jude Terror Programming 8 12-11-2008 11:03 AM
converting fake hex to decimal in c acid_kewpie Programming 10 08-20-2003 02:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration