Decoding URLs

Sometimes when URLs have encoded arguments, it’s hard to figure out what’s in it.

For instance the following encoded URL:

%3Cogc%3AFilter%3E%3CAnd%3E%3Cogc%3APropertyIsEqualTo%3E%3Cogc%3APropertyName%3EGsSoil.Portugal%3ACLASSE%3C%2Fogc%3APropertyName%3E%3Cogc%3ALiteral%3EClasse+F%3C%2Fogc%3ALiteral%3E%3C%2Fogc%3APropertyIsEqualTo%3E%3Cogc%3APropertyIsEqualTo%3E%3Cogc%3APropertyName%3EGsSoil.Portugal%3ACODUSO%3C%2Fogc%3APropertyName%3E%3Cogc%3ALiteral%3E11%3C%2Fogc%3ALiteral%3E%3C%2Fogc%3APropertyIsEqualTo%3E%3CIntersects%3E%3Cogc%3APropertyName%3EGsSoil.Portugal%3Athe_geom%3C%2Fogc%3APropertyName%3E%3Cgml%3APolygon+srsName%3D%22EPSG%3A4326%22%3E%3Cgml%3AouterBoundaryIs%3E%3Cgml%3ALinearRing%3E%3Cgml%3Acoordinates%3E-8.956%2C39.369+-8.659%2C39.499+-8.538%2C39.068+-8.995%2C39.059+-9.023%2C39.23+-8.956%2C39.369%3C%2Fgml%3Acoordinates%3E%3C%2Fgml%3ALinearRing%3E%3C%2Fgml%3AouterBoundaryIs%3E%3C%2Fgml%3APolygon%3E%3C%2FIntersects%3E%3C%2FAnd%3E%3C%2Fogc%3AFilter%3E
can be decoded with the following bash script using sed:
    #!/bin/sh
    # See http://superuser.com/questions/76612/
    read stdin
    echo -n -e "$(echo $stdin | sed 's/+/ /g;s/%\(..\)/\\x\1/g;')"

If this script is saved as “urldecode.sh”, it can be used as:

    cat my_encoded_url.txt |  ./urldecode.sh

or

    echo '...encodedurl...' |  sh ./urldecode.sh