URL decoding with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers URL decoding with awk
# 1  
Old 03-01-2012
URL decoding with awk

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.

Code:
_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!

Last edited by uiop44; 03-01-2012 at 12:57 AM..
# 2  
Old 03-01-2012
What's your system? What's your shell?

I'd try converting %02 into \x02, then feeding it through echo -e, but that's a Linux/BASH thing...
# 3  
Old 03-01-2012
I had another go at this without using xxd.

This version uses only sh builtins, sed, jot/seq and dc.

Not thoroughly tested, but seems to work.

Handling unicode too would be useful.

Code:
# octal to hex #
o2h(){ 
echo 16o 8i $1 p|dc;
}

# print an ascii table #
ud1(){ 
{
case $(uname) in 
FreeBSD)
jot -w%03d 178 000 177;
\
 ;;
*)
seq -w 000 177;
\
 ;;
esac;
} \
 |while read a;do 
printf '\'$a'\040'$a'\n' ;
done \
 |sed '
1,/039/d;
/[89]$/d;
/[89].$/d;
$d;
/^ /s/ /+/;
/^$/d;
'

}

# rough sed script to fd1 #
ud2(){
sed 's,.* ,,' \
| while read a;
do 
printf 'sl%%';
printf $(o2h $a);
printf 'l'
printf '\'$a;
printf 'lg\n';
done;
}

# add escapes or quotes \
 # for certain specials chars \
 # and delete single quote hex 27 #
ud3(){
sed '
/20/s/ /+/;
/6C/{
s,l,/,;
s,ll,/l,;
s,lg,/g,;
};
/5C/s,.,\"&\",7;
/22/s,\",\\",;
/26/s,&,\\&,;
/27/d;
';
}

# add single quote hex 27 #
ud4(){
printf 'sl%%27l\047\"\047\"\047lg\n';
}

# function header #
ud5(){
echo "ud(){
sed '";
}

# function footer #
ud6(){
echo "'
}"
}

# put it all together #
ud7(){ 
{
ud5;
ud1 \
 |ud2 \
 |ud3;
ud4;
ud6;
};
}


Last edited by uiop44; 03-04-2012 at 09:58 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading URL using Mechanize and dump all the contents of the URL to a file

Hello, Am very new to perl , please help me here !! I need help in reading a URL from command line using PERL:: Mechanize and needs all the contents from the URL to get into a file. below is the script which i have written so far , #!/usr/bin/perl use LWP::UserAgent; use... (2 Replies)
Discussion started by: scott_cog
2 Replies

2. UNIX for Dummies Questions & Answers

Decoding a string

Hi, If my input string is 3a3b4c then my result should be aaabbbcccc. Please guide me how to achieve this in a bash script. Thanks (18 Replies)
Discussion started by: pandeesh
18 Replies

3. UNIX for Dummies Questions & Answers

Awk: print all URL addresses between iframe tags without repeating an already printed URL

Here is what I have so far: find . -name "*php*" -or -name "*htm*" | xargs grep -i iframe | awk -F'"' '/<iframe*/{gsub(/.\*iframe>/,"\"");print $2}' Here is an example content of a PHP or HTM(HTML) file: <iframe src="http://ADDRESS_1/?click=5BBB08\" width=1 height=1... (18 Replies)
Discussion started by: striker4o
18 Replies

4. Web Development

Regex to rewrite URL to another URL based on HTTP_HOST?

I am trying to find a way to test some code, but I need to rewrite a specific URL only from a specific HTTP_HOST The call goes out to http://SUB.DOMAIN.COM/showAssignment/7bde10b45efdd7a97629ef2fe01f7303/jsmodule/Nevow.Athena The ID in the middle is always random due to the cookie. I... (5 Replies)
Discussion started by: EXT3FSCK
5 Replies

5. Shell Programming and Scripting

Extract URL from RSS Feed in AWK

Hi, I have following data file; <outline title="Matt Cutts" type="rss" version="RSS" xmlUrl="http://www.mattcutts.com/blog/feed/" htmlUrl="http://www.mattcutts.com/blog"/> <outline title="Stone" text="Stone" type="rss" version="RSS" xmlUrl="http://feeds.feedburner.com/STC-Art"... (8 Replies)
Discussion started by: fahdmirza
8 Replies

6. Shell Programming and Scripting

how to judge wether a url is valid or not using awk

rt 3ks:confused: (6 Replies)
Discussion started by: rainboisterous
6 Replies

7. Shell Programming and Scripting

decoding URL encoded strings

Hi, I have a couple pages of URL encoded strings that I need to unencode (they were originally in Arabic). So the first step is to unencode the strings and then to translate them to English. They are actually lists of words so the translation from Arabic to English shouldn't be too complicated.... (1 Reply)
Discussion started by: ed111
1 Replies

8. UNIX for Dummies Questions & Answers

ReDirecting a URL to another URL - Linux

Hello, I need to redirect an existing URL, how can i do that? There's a current web address to a GUI that I have to redirect to another webaddress. Does anyone know how to do this? This is on Unix boxes Linux. example: https://m45.testing.address.net/host.php make it so the... (3 Replies)
Discussion started by: SkySmart
3 Replies

9. Shell Programming and Scripting

url calling and parameter passing to url in script

Hi all, I need to write a unix script in which need to call a url. Then need to pass parameters to that url. please help. Regards, gander_ss (1 Reply)
Discussion started by: gander_ss
1 Replies

10. UNIX for Advanced & Expert Users

url calling and parameter passing to url in script

Hi all, I need to write a unix script in which need to call a url. Then need to pass parameters to that url. please help. Regards, gander_ss (1 Reply)
Discussion started by: gander_ss
1 Replies
Login or Register to Ask a Question