views:

37

answers:

2

Edited to correct the question

I'm using awk to urldecode some text.

If I code the string into the printf statement like so: printf "%s", "\x3D" it correctly outputs =. The same if I have the whole escaped string as a variable.

However, if I only have the 3D, how can I append the \x so printf will print the = and not \x3D.

I'm using busybox awk 1.4.2 and the ash shell.

A: 

I don't know how you do this in awk, but it's trivial in perl:

echo "http://example.com/?q=foo%3Dbar" | 
    perl -pe 's/+/ /g; s/%([0-9a-f]{2})/chr(hex($1))/eig'
Zack
Thanks, but perl isn't available.
Johan
A: 

Since you're using ash and Perl isn't available, I'm assuming that you may not have gawk.

For me, using gawk or busybox awk, your second example works the same as the first (I get "=" from both) unless I use the --posix option (in which case I get "x3D" for both).

If I use --non-decimal-data or --traditional with gawk I get "=".

What version of AWK are you using (awk, nawk, gawk, busybox - and version number)?

Edit:

You can coerce the variable's string value into a numeric one by adding zero:

~/busybox/awk 'BEGIN { string="3D"; pre="0x"; hex=pre string; printf "%c", hex+0}'
Dennis Williamson
You'r right, it does work. I asked the wrong question - I'll amend it. (I'm using busybox awk, version 1.4.2)
Johan
@Johan: See my edit.
Dennis Williamson