Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode URL-encoded string in shell?

I have a file with a list of user-agents which are encoded. E.g.:

Mozilla%2F5.0%20%28Macintosh%3B%20U%3B%20Intel%20Mac%20OS%20X%2010.6%3B%20en 

I want a shell script which can read this file and write to a new file with decoded strings.

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en 

I have been trying to use this example to get it going but it is not working so far.

$ echo -e "$(echo "%31+%32%0A%33+%34" | sed 'y/+/ /; s/%/\\x/g')" 

My script looks like:

#!/bin/bash for f in *.log; do   echo -e "$(cat $f | sed 'y/+/ /; s/%/\x/g')" > y.log done 
like image 543
user785717 Avatar asked Jun 06 '11 10:06

user785717


People also ask

How do you decode an encoded string?

To encode a string we need encodeURIComponent() or encodeURI() and to decode a string we need decodeURIComponent() or decodeURI(). Initially, we have used escape() to encode a string but since it is deprecated we are now using encodeURI().

Does curl do URL encoding?

To help you send data you have not already encoded, curl offers the --data-urlencode option. This option offers several different ways to URL encode the data you give it. You use it like --data-urlencode data in the same style as the other --data options.


2 Answers

Here is a simple one-line solution.

$ function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; } 

It may look like perl :) but it is just pure bash. No awks, no seds ... no overheads. Using the : builtin, special parameters, pattern substitution and the echo builtin's -e option to translate hex codes into characters. See bash's manpage for further details. You can use this function as separate command

$ urldecode https%3A%2F%2Fgoogle.com%2Fsearch%3Fq%3Durldecode%2Bbash https://google.com/search?q=urldecode+bash 

or in variable assignments, like so:

$ x="http%3A%2F%2Fstackoverflow.com%2Fsearch%3Fq%3Durldecode%2Bbash" $ y=$(urldecode "$x") $ echo "$y" http://stackoverflow.com/search?q=urldecode+bash 
like image 94
guest Avatar answered Oct 09 '22 20:10

guest


If you are a python developer, this maybe preferable:

For Python 3.x(default):

echo -n "%21%20" | python3 -c "import sys; from urllib.parse import unquote; print(unquote(sys.stdin.read()));" 

For Python 2.x(deprecated):

echo -n "%21%20" | python -c "import sys, urllib as ul; print ul.unquote(sys.stdin.read());" 

urllib is really good at handling URL parsing

like image 36
Jay Avatar answered Oct 09 '22 21:10

Jay