The challenge:
Decode URL's, i.e. convert %HEX to the corresponding special characters, using only UNIX base utilities, and without having to type out each special character.
I have an anonymous C code snippet where the author assigns each hex digit a number from 0 to 16 and then does some bit shifting. It works nicely.
Is there an easy way to do this with awk?
Wanna see an ugly hack? Well, here it is anyway. This attempt outputs a shell function called "ud" that purports to decode URL's piped through it. Not tested thoroughly.
Maybe this could be done with dc or bc (for hex->octal, then use awk for gsub) instead of xxd.
_ud(){
{
jot -w"printf '\%03d'|xxd -u" 178 000 177|sh ;
jot -w"printf '\%03d'|xxd -u" 178 000 177|sh\
|dd conv=lcase 2>&- ;
} \
|sed -e '
s/.*0: //;
/[A-Za-z0-9]$/d;
/^[01]/d;' \
-e ':a' \
-e 's/ / /;t a' \
-e '
s, ,l,;
s,.*,sl%&lg,;
/2[Ff]/!s,ll,l+l,;
/5[Cc]/s,.,\"&\",7;
/22/s,\",\\",;
/26/s,&,\\&,;
/27/d;
'
printf 'sl%%27l\047\"\047\"\047lg\n';
printf 's/&/\&/g\n'
}
ud(){
_ud \
|sed '
1s,.*,ud(){\
sed '"'"'\
&,;
$s,.*,&\
'"'"';\
},
'
}
Yikes!