This one is the fastest of them all by a large margin and it doesn t need gawk:
#!/usr/bin/mawk -f
function decode_url(url, dec, tmp, pre, mid, rep) {
tmp = url
while (match(tmp, /\%[0-9a-zA-Z][0-9a-zA-Z]/)) {
pre = substr(tmp, 1, RSTART - 1)
mid = substr(tmp, RSTART + 1, RLENGTH - 1)
rep = sprintf("%c", ("0x" mid) + 0)
dec = dec pre rep
tmp = substr(tmp, RSTART + RLENGTH)
}
return dec tmp
}
{
print decode_url($0)
}
Save it as decode_url.awk
and use it like you normally would. E.g:
$ ./decode_url.awk <<< Hello%2C%20world%20%21
Hello, world !
But if you want an even faster version:
#!/usr/bin/mawk -f
function gen_url_decode_array( i, n, c) {
delete decodeArray
for (i = 32; i < 64; ++i) {
c = sprintf("%c", i)
n = sprintf("%%%02X", i)
decodeArray[n] = c
decodeArray[tolower(n)] = c
}
}
function decode_url(url, dec, tmp, pre, mid, rep) {
tmp = url
while (match(tmp, /\%[0-9a-zA-Z][0-9a-zA-Z]/)) {
pre = substr(tmp, 1, RSTART - 1)
mid = substr(tmp, RSTART, RLENGTH)
rep = decodeArray[mid]
dec = dec pre rep
tmp = substr(tmp, RSTART + RLENGTH)
}
return dec tmp
}
BEGIN {
gen_url_decode_array()
}
{
print decode_url($0)
}
Other interpreters than mawk
should have no problem with them.