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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-14-2010, 07:03 AM
|
#1
|
LQ Newbie
Registered: Apr 2004
Posts: 25
Rep:
|
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!
|
|
|
12-14-2010, 07:19 AM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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.
|
|
|
12-14-2010, 07:45 AM
|
#3
|
LQ Newbie
Registered: Apr 2004
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by catkin
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?
|
|
|
12-14-2010, 08:06 AM
|
#4
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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]
|
|
|
12-14-2010, 08:35 AM
|
#5
|
LQ Newbie
Registered: Apr 2004
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by catkin
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
|
|
|
12-14-2010, 08:45 AM
|
#6
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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
|
|
|
12-14-2010, 08:50 AM
|
#7
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017
|
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}'
|
|
|
12-14-2010, 09:16 AM
|
#8
|
LQ Newbie
Registered: Apr 2004
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by catkin
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!
|
|
|
12-14-2010, 09:18 AM
|
#9
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Ah grail How you doing? Good you've dropped in. What does 0x$7 become that strtonum converts it to 8?
|
|
|
12-14-2010, 09:53 AM
|
#10
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017
|
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.
|
12-14-2010, 12:06 PM
|
#11
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Thanks grail It's so simple now you have explained it.
|
|
|
12-14-2010, 08:19 PM
|
#12
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
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.
|
|
|
All times are GMT -5. The time now is 02:58 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|