![]() $ urlencode "Проба пера/Pen test" | urldecode So, finally urlencode alias in bash looks like this: alias urlencode='python -c "import urllib, sys print urllib.quote(sys.argv if len(sys.argv) > 1 else (), \"\")"'Įxample $ urlencode "Проба пера/Pen test" If you also need to encode the slash, just add an empty second argument to the quote function, then the slash will also be encoded. Personally, I use these aliases for URL encoding and decoding: alias urlencode='python -c "import urllib, sys print urllib.quote( sys.argv if len(sys.argv) > 1 else ())"'Īlias urldecode='python -c "import urllib, sys print urllib.unquote(sys.argv if len(sys.argv) > 1 else ())"'īoth commands allow you to convert data, passed as a command line argument or read it from standard input, because both one-liners check whether there are command line arguments (even empty ones) and process them or just read standard input otherwise. I can't comment on best answer in this thread, so here is mine. Unmodified, with the exception that + is converted to space.Įxample of decoding URL: $ urlencode -d "http%3a%2f%%2f" %HH and %hh strings are converted and other characters are passed through dĭo URL-decoding rather than encoding, according to RFC 1738. This results in slightly more human-readable stringsīut the application must be prepared to create or simulate = - _ and / are passed through unmodified. ![]() ![]() Instead of full conversion, do GridSite "mild URL encoding" They are concatenated with separating spaces before conversion. Where HH is their two-digit upper-case hexadecimal ASCII representation.įor example, the URL becomes http%3A%2F%2Furlencode converts each character in all the strings Passed through unmodified, but all other characters are represented as %HH, Urlencode encodes strings according to RFC 1738. Urlencode - convert strings to or from URL-encoded form Then run sed -f sedscript new, which will output as you desired.įor an ease, the command urlencode is also available directly in gridsite-clients package can be installed from (by sudo apt-get install gridsite-clients in Ubuntu/Debian system). See Using awk printf to urldecode text for examples with common awk implementations, including BusyBox. If you want to stick to POSIX portable tools, it's awkward, because the only serious candidate is awk, which doesn't parse hexadecimal numbers. Or to process a file: perl -pli -MURI::Escape -e '$_ = uri_unescape($_)' file decoded_url=$(perl -MURI::Escape -e 'print uri_unescape($ARGV)' "$encoded_url") Or to process a file: python3 -c 'import sys, urllib.parse print((()))' file.new & decoded_url=$(python3 -c 'import sys, urllib.parse print((sys.argv))' "$encoded_url") Or to process a file: python2 -c 'import sys, urllib print urllib.unquote(())' file.new & decoded_url=$(python2 -c 'import sys, urllib print urllib.unquote(sys.argv)' "$encoded_url") There is a built-in function for that in the Python standard library. How can I encode and decode percent-encoded strings on the command line? at Ask Ubuntu.How to decode URL-encoded string in shell? at SO.How to remove URI encoding from file names?.Can wget decode uri file names when downloading in batch?.If you need to remove url encoding from the file names, use deurlname tool from renameutils (e.g. However above syntax won't handle pluses ( +) correctly, so you've to replace them with spaces via sed or as suggested by use the following syntax: decoded=$(input=$' When scripting, you can use the following syntax: input="http%3A%2F%2Fwww" Then every time when you need it, simply go with: $ echo "http%3A%2F%2Fwww" | urldecode You may define it as alias and add it to your shell rc files: $ alias urldecode='sed | xargs -0 printf "%b"' ![]() Note: The above syntax may not convert + to spaces, and can eat all the newlines. Or the following alternative using echo -e: $ sed -e's/%\(\)/\\\\\x\1/g' file | xargs echo -e Try the following command line: $ sed file | xargs -0 printf "%b" ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |