#!/bin/bash
function urlencode() {
LC_ALL=C awk 'BEGIN {
for (i = 0; i <= 255; i++)
hex[sprintf("%c", i)] = sprintf("%%%02X", i)
safe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.:/!~*@$&[]+=,;?"
split(safe, safechars, "")
for (i in safechars)
hex[safechars[i]] = safechars[i]
}
{
encoded = ""
for (i = 1; i <= length($0); i++)
encoded = encoded hex[substr($0, i, 1)]
print encoded
}
'
}
This little bad boy rivals the speed of my C implementation below!
Slower by less than 10%.
[Btw: this example is wrapped in a Bash function for ease of use]