How can I encode and decode percent-encoded strings on the command line?
How can I encode and decode percent-encoded (URL encoded) strings on the command line?
I'm looking for a solution that can do this:
$ percent-encode "ændrük"
%C3%A6ndr%C3%BCk
$ percent-decode "%C3%A6ndr%C3%BCk"
ændrük
command-line conversion text encoding url
add a comment |
How can I encode and decode percent-encoded (URL encoded) strings on the command line?
I'm looking for a solution that can do this:
$ percent-encode "ændrük"
%C3%A6ndr%C3%BCk
$ percent-decode "%C3%A6ndr%C3%BCk"
ændrük
command-line conversion text encoding url
Do you want to incorporate different encodings too?%E6ndr%FCkdoesn't look like (standard) UTF8 to me. Or it's just an example?
– arrange
Jul 19 '11 at 21:13
@arrange Thanks for catching that. Apparently I chose the bad apple among search results for online converters.
– ændrük
Jul 19 '11 at 21:49
For file names, see: How to remove URI encoding in file names.
– kenorb
Feb 12 '15 at 17:54
add a comment |
How can I encode and decode percent-encoded (URL encoded) strings on the command line?
I'm looking for a solution that can do this:
$ percent-encode "ændrük"
%C3%A6ndr%C3%BCk
$ percent-decode "%C3%A6ndr%C3%BCk"
ændrük
command-line conversion text encoding url
How can I encode and decode percent-encoded (URL encoded) strings on the command line?
I'm looking for a solution that can do this:
$ percent-encode "ændrük"
%C3%A6ndr%C3%BCk
$ percent-decode "%C3%A6ndr%C3%BCk"
ændrük
command-line conversion text encoding url
command-line conversion text encoding url
edited Jul 19 '11 at 21:45
ændrük
asked Jul 19 '11 at 20:23
ændrükændrük
42.1k61195342
42.1k61195342
Do you want to incorporate different encodings too?%E6ndr%FCkdoesn't look like (standard) UTF8 to me. Or it's just an example?
– arrange
Jul 19 '11 at 21:13
@arrange Thanks for catching that. Apparently I chose the bad apple among search results for online converters.
– ændrük
Jul 19 '11 at 21:49
For file names, see: How to remove URI encoding in file names.
– kenorb
Feb 12 '15 at 17:54
add a comment |
Do you want to incorporate different encodings too?%E6ndr%FCkdoesn't look like (standard) UTF8 to me. Or it's just an example?
– arrange
Jul 19 '11 at 21:13
@arrange Thanks for catching that. Apparently I chose the bad apple among search results for online converters.
– ændrük
Jul 19 '11 at 21:49
For file names, see: How to remove URI encoding in file names.
– kenorb
Feb 12 '15 at 17:54
Do you want to incorporate different encodings too?
%E6ndr%FCk doesn't look like (standard) UTF8 to me. Or it's just an example?– arrange
Jul 19 '11 at 21:13
Do you want to incorporate different encodings too?
%E6ndr%FCk doesn't look like (standard) UTF8 to me. Or it's just an example?– arrange
Jul 19 '11 at 21:13
@arrange Thanks for catching that. Apparently I chose the bad apple among search results for online converters.
– ændrük
Jul 19 '11 at 21:49
@arrange Thanks for catching that. Apparently I chose the bad apple among search results for online converters.
– ændrük
Jul 19 '11 at 21:49
For file names, see: How to remove URI encoding in file names.
– kenorb
Feb 12 '15 at 17:54
For file names, see: How to remove URI encoding in file names.
– kenorb
Feb 12 '15 at 17:54
add a comment |
8 Answers
8
active
oldest
votes
These commands do what you want:
python -c "import urllib, sys; print urllib.quote(sys.argv[1])" æ
python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" %C3%A6
If you want to encode spaces as +, replace urllib.quote with urllib.quote_plus.
I'm guessing you will want to alias them ;-)
1
What is that æ character at the end of first line? Edit: answering to myself - got it, it's just a single character UTF8 to-be-encoded string for example purpose :-)
– TMG
Jan 8 '18 at 10:03
1
how about python3?
– RicardoE
Aug 25 '18 at 5:37
@RicardoE check this answer.
– Pablo Bianchi
Jan 17 at 19:03
add a comment |
shell
Try the following command line:
$ echo "%C3%A6ndr%C3%BCk" | sed 's@+@ @g;s@%@\x@g' | xargs -0 printf "%b"
ændrük
You may define it as alias and add it to your shell rc files:
$ alias urldecode='sed "s@+@ @g;s@%@\\x@g" | xargs -0 printf "%b"'
Then every time when you need it, simply go with:
$ echo "http%3A%2F%2Fwww" | urldecode
http://www
bash
When scripting, you can use the following syntax:
input="http%3A%2F%2Fwww"
decoded=$(printf '%b' "${input//%/\x}")
However above syntax won't handle pluses (+) correctly, so you've to replace them with spaces via sed.
You can also use the following urlencode() and urldecode() functions:
urlencode() {
# urlencode <string>
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\x}"
}
Note that your urldecode() assumes the data contains no backslash.
bash + xxd
Bash function with xxd tool:
urlencode() {
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done
esac
done
}
Found in cdown's gist file, also at stackoverflow.
Python
Try to define the following aliases:
alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
Usage:
$ urlencode "ændrük"
C%26ndrC%3Ck
$ urldecode "%C3%A6ndr%C3%BCk"
ændrük
Source: ruslanspivak
PHP
Using PHP you can try the following command:
$ echo oil+and+gas | php -r 'echo urldecode(fgets(STDIN));' // Or: php://stdin
oil and gas
or just:
php -r 'echo urldecode("oil+and+gas");'
Use -R for multiple line input.
Perl
In Perl you can use URI::Escape.
decoded_url=$(perl -MURI::Escape -e 'print uri_unescape($ARGV[0])' "$encoded_url")
Or to process a file:
perl -i -MURI::Escape -e 'print uri_unescape($ARGV[0])' file
sed
Using sed can be achieved by:
cat file | sed -e's/%([0-9A-F][0-9A-F])/\\x1/g' | xargs echo -e
awk
Try anon solution:
awk -niord '{printf RT?$0chr("0x"substr(RT,2)):$0}' RS=%..
See: Using awk printf to urldecode text.
decoding file names
If you need to remove url encoding from the file names, use deurlname tool from renameutils (e.g. deurlname *.*).
See also:
- Can wget decode uri file names when downloading in batch?
- How to remove URI encoding from file names?
Related:
How to decode URL-encoded string in shell? at SO
Decoding URL encoding (percent encoding) at unix SE
add a comment |
Percent-encode reserved URI characters and non-ASCII characters
jq -s -R -r @uri
-s (--slurp) reads input lines into an array and -s -R (--slurp --raw-input) reads the input into a single string. -r (--raw-output) outputs the contents of strings instead of JSON string literals.
Percent-encode all characters
xxd -p|tr -d \n|sed 's/../%&/g'
tr -d \n removes the linefeeds that are added by xxd -p after every 60 characters.
Percent-encode all characters except ASCII alphanumeric characters in Bash
eu () {
local LC_ALL=C c
while IFS= read -r -n1 -d '' c
do
if [[ $c = [[:alnum:]] ]]
then
printf %s "$c"
else
printf %%%02x "'$c"
fi
done
}
Without -d '' this would skip linefeeds and null bytes. Without IFS= this would replace characters in IFS with %00. Without LC_ALL=C this would for example replace あ with %3042 in a UTF-8 locale.
add a comment |
Pure bash solution for decoding only:
$ a='%C3%A6ndr%C3%BCk'
$ echo -e "${a//%/\x}"
ændrük
add a comment |
I can't comment on best answer in this thread, so here is mine.
Personally, I use these aliases for URL encoding and decoding:
alias urlencode='python -c "import urllib, sys; print urllib.quote( sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
alias urldecode='python -c "import urllib, sys; print urllib.unquote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
Both 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.
update 2015-07-16 (empty 1st arg)
... according to @muru comment.
update 2017-05-28 (slash encoding)
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.
So, finally urlencode alias in bash looks like this:
alias urlencode='python -c "import urllib, sys; print urllib.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], "")"'
Example
$ urlencode "Проба пера/Pen test"
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ echo "Проба пера/Pen test" | urlencode
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ urldecode %D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
Проба пера/Pen test
$ echo "%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test" | urldecode
Проба пера/Pen test
$ urlencode "Проба пера/Pen test" | urldecode
Проба пера/Pen test
$ echo "Проба пера/Pen test" | urlencode | urldecode
Проба пера/Pen test
1
I thinksys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]might be more appropriate. Especially if you use this in scripts and accidentally give an empty first argument.
– muru
Jul 16 '15 at 2:16
As per @muru comment I changed the checking for an argument on the command line. It was:len(sys.argv) < 2 and sys.stdin.read()[0:-1] or sys.argv[1]Now:sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]That is, if there is even an empty first argument, the command does not wait for input from the standard input, but processes an empty argument.
– DIG mbl
Jul 16 '15 at 13:56
add a comment |
I found a package, renameutils, that contain the utility deurlname that is able to rename a file containing "percent-encoded" characters.
Unfortunately, it does not decode stdin or a command line option, but only rename a file, so you have to create a dummy file to obtain the decoding (the name of the renamed file), but with some bash scripting the process can be automated.
No information about the encoding part, even because it could be questionable which characters to encode. Only non-ASCII?
I think there should be some better tool/method.
add a comment |
Here is a POSIX Awk function for encoding:
function encodeURIComponent(str, j, q) {
while (y++ < 125) z[sprintf("%c", y)] = y
while (y = substr(str, ++j, 1))
q = y ~ /[[:alnum:]_.!~*47()-]/ ? q y : q sprintf("%%%02X", z[y])
return q
}
Example
add a comment |
Similar to Stefano ansqer but in Python 3:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" æ
python -c "import urllib.parse, sys; print(urllib.parse.unquote(sys.argv[1]))" %C3%A6
To encode also slashes:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], ""))"
More info about the difference here.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f53770%2fhow-can-i-encode-and-decode-percent-encoded-strings-on-the-command-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
These commands do what you want:
python -c "import urllib, sys; print urllib.quote(sys.argv[1])" æ
python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" %C3%A6
If you want to encode spaces as +, replace urllib.quote with urllib.quote_plus.
I'm guessing you will want to alias them ;-)
1
What is that æ character at the end of first line? Edit: answering to myself - got it, it's just a single character UTF8 to-be-encoded string for example purpose :-)
– TMG
Jan 8 '18 at 10:03
1
how about python3?
– RicardoE
Aug 25 '18 at 5:37
@RicardoE check this answer.
– Pablo Bianchi
Jan 17 at 19:03
add a comment |
These commands do what you want:
python -c "import urllib, sys; print urllib.quote(sys.argv[1])" æ
python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" %C3%A6
If you want to encode spaces as +, replace urllib.quote with urllib.quote_plus.
I'm guessing you will want to alias them ;-)
1
What is that æ character at the end of first line? Edit: answering to myself - got it, it's just a single character UTF8 to-be-encoded string for example purpose :-)
– TMG
Jan 8 '18 at 10:03
1
how about python3?
– RicardoE
Aug 25 '18 at 5:37
@RicardoE check this answer.
– Pablo Bianchi
Jan 17 at 19:03
add a comment |
These commands do what you want:
python -c "import urllib, sys; print urllib.quote(sys.argv[1])" æ
python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" %C3%A6
If you want to encode spaces as +, replace urllib.quote with urllib.quote_plus.
I'm guessing you will want to alias them ;-)
These commands do what you want:
python -c "import urllib, sys; print urllib.quote(sys.argv[1])" æ
python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" %C3%A6
If you want to encode spaces as +, replace urllib.quote with urllib.quote_plus.
I'm guessing you will want to alias them ;-)
edited Apr 13 '17 at 12:25
Community♦
1
1
answered Jul 19 '11 at 21:22
Stefano Palazzo♦Stefano Palazzo
63.4k33183216
63.4k33183216
1
What is that æ character at the end of first line? Edit: answering to myself - got it, it's just a single character UTF8 to-be-encoded string for example purpose :-)
– TMG
Jan 8 '18 at 10:03
1
how about python3?
– RicardoE
Aug 25 '18 at 5:37
@RicardoE check this answer.
– Pablo Bianchi
Jan 17 at 19:03
add a comment |
1
What is that æ character at the end of first line? Edit: answering to myself - got it, it's just a single character UTF8 to-be-encoded string for example purpose :-)
– TMG
Jan 8 '18 at 10:03
1
how about python3?
– RicardoE
Aug 25 '18 at 5:37
@RicardoE check this answer.
– Pablo Bianchi
Jan 17 at 19:03
1
1
What is that æ character at the end of first line? Edit: answering to myself - got it, it's just a single character UTF8 to-be-encoded string for example purpose :-)
– TMG
Jan 8 '18 at 10:03
What is that æ character at the end of first line? Edit: answering to myself - got it, it's just a single character UTF8 to-be-encoded string for example purpose :-)
– TMG
Jan 8 '18 at 10:03
1
1
how about python3?
– RicardoE
Aug 25 '18 at 5:37
how about python3?
– RicardoE
Aug 25 '18 at 5:37
@RicardoE check this answer.
– Pablo Bianchi
Jan 17 at 19:03
@RicardoE check this answer.
– Pablo Bianchi
Jan 17 at 19:03
add a comment |
shell
Try the following command line:
$ echo "%C3%A6ndr%C3%BCk" | sed 's@+@ @g;s@%@\x@g' | xargs -0 printf "%b"
ændrük
You may define it as alias and add it to your shell rc files:
$ alias urldecode='sed "s@+@ @g;s@%@\\x@g" | xargs -0 printf "%b"'
Then every time when you need it, simply go with:
$ echo "http%3A%2F%2Fwww" | urldecode
http://www
bash
When scripting, you can use the following syntax:
input="http%3A%2F%2Fwww"
decoded=$(printf '%b' "${input//%/\x}")
However above syntax won't handle pluses (+) correctly, so you've to replace them with spaces via sed.
You can also use the following urlencode() and urldecode() functions:
urlencode() {
# urlencode <string>
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\x}"
}
Note that your urldecode() assumes the data contains no backslash.
bash + xxd
Bash function with xxd tool:
urlencode() {
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done
esac
done
}
Found in cdown's gist file, also at stackoverflow.
Python
Try to define the following aliases:
alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
Usage:
$ urlencode "ændrük"
C%26ndrC%3Ck
$ urldecode "%C3%A6ndr%C3%BCk"
ændrük
Source: ruslanspivak
PHP
Using PHP you can try the following command:
$ echo oil+and+gas | php -r 'echo urldecode(fgets(STDIN));' // Or: php://stdin
oil and gas
or just:
php -r 'echo urldecode("oil+and+gas");'
Use -R for multiple line input.
Perl
In Perl you can use URI::Escape.
decoded_url=$(perl -MURI::Escape -e 'print uri_unescape($ARGV[0])' "$encoded_url")
Or to process a file:
perl -i -MURI::Escape -e 'print uri_unescape($ARGV[0])' file
sed
Using sed can be achieved by:
cat file | sed -e's/%([0-9A-F][0-9A-F])/\\x1/g' | xargs echo -e
awk
Try anon solution:
awk -niord '{printf RT?$0chr("0x"substr(RT,2)):$0}' RS=%..
See: Using awk printf to urldecode text.
decoding file names
If you need to remove url encoding from the file names, use deurlname tool from renameutils (e.g. deurlname *.*).
See also:
- Can wget decode uri file names when downloading in batch?
- How to remove URI encoding from file names?
Related:
How to decode URL-encoded string in shell? at SO
Decoding URL encoding (percent encoding) at unix SE
add a comment |
shell
Try the following command line:
$ echo "%C3%A6ndr%C3%BCk" | sed 's@+@ @g;s@%@\x@g' | xargs -0 printf "%b"
ændrük
You may define it as alias and add it to your shell rc files:
$ alias urldecode='sed "s@+@ @g;s@%@\\x@g" | xargs -0 printf "%b"'
Then every time when you need it, simply go with:
$ echo "http%3A%2F%2Fwww" | urldecode
http://www
bash
When scripting, you can use the following syntax:
input="http%3A%2F%2Fwww"
decoded=$(printf '%b' "${input//%/\x}")
However above syntax won't handle pluses (+) correctly, so you've to replace them with spaces via sed.
You can also use the following urlencode() and urldecode() functions:
urlencode() {
# urlencode <string>
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\x}"
}
Note that your urldecode() assumes the data contains no backslash.
bash + xxd
Bash function with xxd tool:
urlencode() {
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done
esac
done
}
Found in cdown's gist file, also at stackoverflow.
Python
Try to define the following aliases:
alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
Usage:
$ urlencode "ændrük"
C%26ndrC%3Ck
$ urldecode "%C3%A6ndr%C3%BCk"
ændrük
Source: ruslanspivak
PHP
Using PHP you can try the following command:
$ echo oil+and+gas | php -r 'echo urldecode(fgets(STDIN));' // Or: php://stdin
oil and gas
or just:
php -r 'echo urldecode("oil+and+gas");'
Use -R for multiple line input.
Perl
In Perl you can use URI::Escape.
decoded_url=$(perl -MURI::Escape -e 'print uri_unescape($ARGV[0])' "$encoded_url")
Or to process a file:
perl -i -MURI::Escape -e 'print uri_unescape($ARGV[0])' file
sed
Using sed can be achieved by:
cat file | sed -e's/%([0-9A-F][0-9A-F])/\\x1/g' | xargs echo -e
awk
Try anon solution:
awk -niord '{printf RT?$0chr("0x"substr(RT,2)):$0}' RS=%..
See: Using awk printf to urldecode text.
decoding file names
If you need to remove url encoding from the file names, use deurlname tool from renameutils (e.g. deurlname *.*).
See also:
- Can wget decode uri file names when downloading in batch?
- How to remove URI encoding from file names?
Related:
How to decode URL-encoded string in shell? at SO
Decoding URL encoding (percent encoding) at unix SE
add a comment |
shell
Try the following command line:
$ echo "%C3%A6ndr%C3%BCk" | sed 's@+@ @g;s@%@\x@g' | xargs -0 printf "%b"
ændrük
You may define it as alias and add it to your shell rc files:
$ alias urldecode='sed "s@+@ @g;s@%@\\x@g" | xargs -0 printf "%b"'
Then every time when you need it, simply go with:
$ echo "http%3A%2F%2Fwww" | urldecode
http://www
bash
When scripting, you can use the following syntax:
input="http%3A%2F%2Fwww"
decoded=$(printf '%b' "${input//%/\x}")
However above syntax won't handle pluses (+) correctly, so you've to replace them with spaces via sed.
You can also use the following urlencode() and urldecode() functions:
urlencode() {
# urlencode <string>
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\x}"
}
Note that your urldecode() assumes the data contains no backslash.
bash + xxd
Bash function with xxd tool:
urlencode() {
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done
esac
done
}
Found in cdown's gist file, also at stackoverflow.
Python
Try to define the following aliases:
alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
Usage:
$ urlencode "ændrük"
C%26ndrC%3Ck
$ urldecode "%C3%A6ndr%C3%BCk"
ændrük
Source: ruslanspivak
PHP
Using PHP you can try the following command:
$ echo oil+and+gas | php -r 'echo urldecode(fgets(STDIN));' // Or: php://stdin
oil and gas
or just:
php -r 'echo urldecode("oil+and+gas");'
Use -R for multiple line input.
Perl
In Perl you can use URI::Escape.
decoded_url=$(perl -MURI::Escape -e 'print uri_unescape($ARGV[0])' "$encoded_url")
Or to process a file:
perl -i -MURI::Escape -e 'print uri_unescape($ARGV[0])' file
sed
Using sed can be achieved by:
cat file | sed -e's/%([0-9A-F][0-9A-F])/\\x1/g' | xargs echo -e
awk
Try anon solution:
awk -niord '{printf RT?$0chr("0x"substr(RT,2)):$0}' RS=%..
See: Using awk printf to urldecode text.
decoding file names
If you need to remove url encoding from the file names, use deurlname tool from renameutils (e.g. deurlname *.*).
See also:
- Can wget decode uri file names when downloading in batch?
- How to remove URI encoding from file names?
Related:
How to decode URL-encoded string in shell? at SO
Decoding URL encoding (percent encoding) at unix SE
shell
Try the following command line:
$ echo "%C3%A6ndr%C3%BCk" | sed 's@+@ @g;s@%@\x@g' | xargs -0 printf "%b"
ændrük
You may define it as alias and add it to your shell rc files:
$ alias urldecode='sed "s@+@ @g;s@%@\\x@g" | xargs -0 printf "%b"'
Then every time when you need it, simply go with:
$ echo "http%3A%2F%2Fwww" | urldecode
http://www
bash
When scripting, you can use the following syntax:
input="http%3A%2F%2Fwww"
decoded=$(printf '%b' "${input//%/\x}")
However above syntax won't handle pluses (+) correctly, so you've to replace them with spaces via sed.
You can also use the following urlencode() and urldecode() functions:
urlencode() {
# urlencode <string>
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\x}"
}
Note that your urldecode() assumes the data contains no backslash.
bash + xxd
Bash function with xxd tool:
urlencode() {
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done
esac
done
}
Found in cdown's gist file, also at stackoverflow.
Python
Try to define the following aliases:
alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
Usage:
$ urlencode "ændrük"
C%26ndrC%3Ck
$ urldecode "%C3%A6ndr%C3%BCk"
ændrük
Source: ruslanspivak
PHP
Using PHP you can try the following command:
$ echo oil+and+gas | php -r 'echo urldecode(fgets(STDIN));' // Or: php://stdin
oil and gas
or just:
php -r 'echo urldecode("oil+and+gas");'
Use -R for multiple line input.
Perl
In Perl you can use URI::Escape.
decoded_url=$(perl -MURI::Escape -e 'print uri_unescape($ARGV[0])' "$encoded_url")
Or to process a file:
perl -i -MURI::Escape -e 'print uri_unescape($ARGV[0])' file
sed
Using sed can be achieved by:
cat file | sed -e's/%([0-9A-F][0-9A-F])/\\x1/g' | xargs echo -e
awk
Try anon solution:
awk -niord '{printf RT?$0chr("0x"substr(RT,2)):$0}' RS=%..
See: Using awk printf to urldecode text.
decoding file names
If you need to remove url encoding from the file names, use deurlname tool from renameutils (e.g. deurlname *.*).
See also:
- Can wget decode uri file names when downloading in batch?
- How to remove URI encoding from file names?
Related:
How to decode URL-encoded string in shell? at SO
Decoding URL encoding (percent encoding) at unix SE
edited May 23 '17 at 12:39
Community♦
1
1
answered May 14 '13 at 13:06
kenorbkenorb
4,55013954
4,55013954
add a comment |
add a comment |
Percent-encode reserved URI characters and non-ASCII characters
jq -s -R -r @uri
-s (--slurp) reads input lines into an array and -s -R (--slurp --raw-input) reads the input into a single string. -r (--raw-output) outputs the contents of strings instead of JSON string literals.
Percent-encode all characters
xxd -p|tr -d \n|sed 's/../%&/g'
tr -d \n removes the linefeeds that are added by xxd -p after every 60 characters.
Percent-encode all characters except ASCII alphanumeric characters in Bash
eu () {
local LC_ALL=C c
while IFS= read -r -n1 -d '' c
do
if [[ $c = [[:alnum:]] ]]
then
printf %s "$c"
else
printf %%%02x "'$c"
fi
done
}
Without -d '' this would skip linefeeds and null bytes. Without IFS= this would replace characters in IFS with %00. Without LC_ALL=C this would for example replace あ with %3042 in a UTF-8 locale.
add a comment |
Percent-encode reserved URI characters and non-ASCII characters
jq -s -R -r @uri
-s (--slurp) reads input lines into an array and -s -R (--slurp --raw-input) reads the input into a single string. -r (--raw-output) outputs the contents of strings instead of JSON string literals.
Percent-encode all characters
xxd -p|tr -d \n|sed 's/../%&/g'
tr -d \n removes the linefeeds that are added by xxd -p after every 60 characters.
Percent-encode all characters except ASCII alphanumeric characters in Bash
eu () {
local LC_ALL=C c
while IFS= read -r -n1 -d '' c
do
if [[ $c = [[:alnum:]] ]]
then
printf %s "$c"
else
printf %%%02x "'$c"
fi
done
}
Without -d '' this would skip linefeeds and null bytes. Without IFS= this would replace characters in IFS with %00. Without LC_ALL=C this would for example replace あ with %3042 in a UTF-8 locale.
add a comment |
Percent-encode reserved URI characters and non-ASCII characters
jq -s -R -r @uri
-s (--slurp) reads input lines into an array and -s -R (--slurp --raw-input) reads the input into a single string. -r (--raw-output) outputs the contents of strings instead of JSON string literals.
Percent-encode all characters
xxd -p|tr -d \n|sed 's/../%&/g'
tr -d \n removes the linefeeds that are added by xxd -p after every 60 characters.
Percent-encode all characters except ASCII alphanumeric characters in Bash
eu () {
local LC_ALL=C c
while IFS= read -r -n1 -d '' c
do
if [[ $c = [[:alnum:]] ]]
then
printf %s "$c"
else
printf %%%02x "'$c"
fi
done
}
Without -d '' this would skip linefeeds and null bytes. Without IFS= this would replace characters in IFS with %00. Without LC_ALL=C this would for example replace あ with %3042 in a UTF-8 locale.
Percent-encode reserved URI characters and non-ASCII characters
jq -s -R -r @uri
-s (--slurp) reads input lines into an array and -s -R (--slurp --raw-input) reads the input into a single string. -r (--raw-output) outputs the contents of strings instead of JSON string literals.
Percent-encode all characters
xxd -p|tr -d \n|sed 's/../%&/g'
tr -d \n removes the linefeeds that are added by xxd -p after every 60 characters.
Percent-encode all characters except ASCII alphanumeric characters in Bash
eu () {
local LC_ALL=C c
while IFS= read -r -n1 -d '' c
do
if [[ $c = [[:alnum:]] ]]
then
printf %s "$c"
else
printf %%%02x "'$c"
fi
done
}
Without -d '' this would skip linefeeds and null bytes. Without IFS= this would replace characters in IFS with %00. Without LC_ALL=C this would for example replace あ with %3042 in a UTF-8 locale.
edited Dec 22 '15 at 4:10
muru
1
1
answered Dec 22 '15 at 2:26
nisetamanisetama
50153
50153
add a comment |
add a comment |
Pure bash solution for decoding only:
$ a='%C3%A6ndr%C3%BCk'
$ echo -e "${a//%/\x}"
ændrük
add a comment |
Pure bash solution for decoding only:
$ a='%C3%A6ndr%C3%BCk'
$ echo -e "${a//%/\x}"
ændrük
add a comment |
Pure bash solution for decoding only:
$ a='%C3%A6ndr%C3%BCk'
$ echo -e "${a//%/\x}"
ændrük
Pure bash solution for decoding only:
$ a='%C3%A6ndr%C3%BCk'
$ echo -e "${a//%/\x}"
ændrük
answered Oct 9 '14 at 19:46
loentarloentar
15114
15114
add a comment |
add a comment |
I can't comment on best answer in this thread, so here is mine.
Personally, I use these aliases for URL encoding and decoding:
alias urlencode='python -c "import urllib, sys; print urllib.quote( sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
alias urldecode='python -c "import urllib, sys; print urllib.unquote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
Both 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.
update 2015-07-16 (empty 1st arg)
... according to @muru comment.
update 2017-05-28 (slash encoding)
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.
So, finally urlencode alias in bash looks like this:
alias urlencode='python -c "import urllib, sys; print urllib.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], "")"'
Example
$ urlencode "Проба пера/Pen test"
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ echo "Проба пера/Pen test" | urlencode
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ urldecode %D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
Проба пера/Pen test
$ echo "%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test" | urldecode
Проба пера/Pen test
$ urlencode "Проба пера/Pen test" | urldecode
Проба пера/Pen test
$ echo "Проба пера/Pen test" | urlencode | urldecode
Проба пера/Pen test
1
I thinksys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]might be more appropriate. Especially if you use this in scripts and accidentally give an empty first argument.
– muru
Jul 16 '15 at 2:16
As per @muru comment I changed the checking for an argument on the command line. It was:len(sys.argv) < 2 and sys.stdin.read()[0:-1] or sys.argv[1]Now:sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]That is, if there is even an empty first argument, the command does not wait for input from the standard input, but processes an empty argument.
– DIG mbl
Jul 16 '15 at 13:56
add a comment |
I can't comment on best answer in this thread, so here is mine.
Personally, I use these aliases for URL encoding and decoding:
alias urlencode='python -c "import urllib, sys; print urllib.quote( sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
alias urldecode='python -c "import urllib, sys; print urllib.unquote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
Both 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.
update 2015-07-16 (empty 1st arg)
... according to @muru comment.
update 2017-05-28 (slash encoding)
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.
So, finally urlencode alias in bash looks like this:
alias urlencode='python -c "import urllib, sys; print urllib.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], "")"'
Example
$ urlencode "Проба пера/Pen test"
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ echo "Проба пера/Pen test" | urlencode
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ urldecode %D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
Проба пера/Pen test
$ echo "%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test" | urldecode
Проба пера/Pen test
$ urlencode "Проба пера/Pen test" | urldecode
Проба пера/Pen test
$ echo "Проба пера/Pen test" | urlencode | urldecode
Проба пера/Pen test
1
I thinksys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]might be more appropriate. Especially if you use this in scripts and accidentally give an empty first argument.
– muru
Jul 16 '15 at 2:16
As per @muru comment I changed the checking for an argument on the command line. It was:len(sys.argv) < 2 and sys.stdin.read()[0:-1] or sys.argv[1]Now:sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]That is, if there is even an empty first argument, the command does not wait for input from the standard input, but processes an empty argument.
– DIG mbl
Jul 16 '15 at 13:56
add a comment |
I can't comment on best answer in this thread, so here is mine.
Personally, I use these aliases for URL encoding and decoding:
alias urlencode='python -c "import urllib, sys; print urllib.quote( sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
alias urldecode='python -c "import urllib, sys; print urllib.unquote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
Both 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.
update 2015-07-16 (empty 1st arg)
... according to @muru comment.
update 2017-05-28 (slash encoding)
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.
So, finally urlencode alias in bash looks like this:
alias urlencode='python -c "import urllib, sys; print urllib.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], "")"'
Example
$ urlencode "Проба пера/Pen test"
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ echo "Проба пера/Pen test" | urlencode
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ urldecode %D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
Проба пера/Pen test
$ echo "%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test" | urldecode
Проба пера/Pen test
$ urlencode "Проба пера/Pen test" | urldecode
Проба пера/Pen test
$ echo "Проба пера/Pen test" | urlencode | urldecode
Проба пера/Pen test
I can't comment on best answer in this thread, so here is mine.
Personally, I use these aliases for URL encoding and decoding:
alias urlencode='python -c "import urllib, sys; print urllib.quote( sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
alias urldecode='python -c "import urllib, sys; print urllib.unquote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])"'
Both 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.
update 2015-07-16 (empty 1st arg)
... according to @muru comment.
update 2017-05-28 (slash encoding)
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.
So, finally urlencode alias in bash looks like this:
alias urlencode='python -c "import urllib, sys; print urllib.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], "")"'
Example
$ urlencode "Проба пера/Pen test"
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ echo "Проба пера/Pen test" | urlencode
%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
$ urldecode %D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test
Проба пера/Pen test
$ echo "%D0%9F%D1%80%D0%BE%D0%B1%D0%B0%20%D0%BF%D0%B5%D1%80%D0%B0%2FPen%20test" | urldecode
Проба пера/Pen test
$ urlencode "Проба пера/Pen test" | urldecode
Проба пера/Pen test
$ echo "Проба пера/Pen test" | urlencode | urldecode
Проба пера/Pen test
edited May 27 '17 at 23:14
answered Jul 15 '15 at 23:47
DIG mblDIG mbl
1537
1537
1
I thinksys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]might be more appropriate. Especially if you use this in scripts and accidentally give an empty first argument.
– muru
Jul 16 '15 at 2:16
As per @muru comment I changed the checking for an argument on the command line. It was:len(sys.argv) < 2 and sys.stdin.read()[0:-1] or sys.argv[1]Now:sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]That is, if there is even an empty first argument, the command does not wait for input from the standard input, but processes an empty argument.
– DIG mbl
Jul 16 '15 at 13:56
add a comment |
1
I thinksys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]might be more appropriate. Especially if you use this in scripts and accidentally give an empty first argument.
– muru
Jul 16 '15 at 2:16
As per @muru comment I changed the checking for an argument on the command line. It was:len(sys.argv) < 2 and sys.stdin.read()[0:-1] or sys.argv[1]Now:sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]That is, if there is even an empty first argument, the command does not wait for input from the standard input, but processes an empty argument.
– DIG mbl
Jul 16 '15 at 13:56
1
1
I think
sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1] might be more appropriate. Especially if you use this in scripts and accidentally give an empty first argument.– muru
Jul 16 '15 at 2:16
I think
sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1] might be more appropriate. Especially if you use this in scripts and accidentally give an empty first argument.– muru
Jul 16 '15 at 2:16
As per @muru comment I changed the checking for an argument on the command line. It was:
len(sys.argv) < 2 and sys.stdin.read()[0:-1] or sys.argv[1] Now: sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1] That is, if there is even an empty first argument, the command does not wait for input from the standard input, but processes an empty argument.– DIG mbl
Jul 16 '15 at 13:56
As per @muru comment I changed the checking for an argument on the command line. It was:
len(sys.argv) < 2 and sys.stdin.read()[0:-1] or sys.argv[1] Now: sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1] That is, if there is even an empty first argument, the command does not wait for input from the standard input, but processes an empty argument.– DIG mbl
Jul 16 '15 at 13:56
add a comment |
I found a package, renameutils, that contain the utility deurlname that is able to rename a file containing "percent-encoded" characters.
Unfortunately, it does not decode stdin or a command line option, but only rename a file, so you have to create a dummy file to obtain the decoding (the name of the renamed file), but with some bash scripting the process can be automated.
No information about the encoding part, even because it could be questionable which characters to encode. Only non-ASCII?
I think there should be some better tool/method.
add a comment |
I found a package, renameutils, that contain the utility deurlname that is able to rename a file containing "percent-encoded" characters.
Unfortunately, it does not decode stdin or a command line option, but only rename a file, so you have to create a dummy file to obtain the decoding (the name of the renamed file), but with some bash scripting the process can be automated.
No information about the encoding part, even because it could be questionable which characters to encode. Only non-ASCII?
I think there should be some better tool/method.
add a comment |
I found a package, renameutils, that contain the utility deurlname that is able to rename a file containing "percent-encoded" characters.
Unfortunately, it does not decode stdin or a command line option, but only rename a file, so you have to create a dummy file to obtain the decoding (the name of the renamed file), but with some bash scripting the process can be automated.
No information about the encoding part, even because it could be questionable which characters to encode. Only non-ASCII?
I think there should be some better tool/method.
I found a package, renameutils, that contain the utility deurlname that is able to rename a file containing "percent-encoded" characters.
Unfortunately, it does not decode stdin or a command line option, but only rename a file, so you have to create a dummy file to obtain the decoding (the name of the renamed file), but with some bash scripting the process can be automated.
No information about the encoding part, even because it could be questionable which characters to encode. Only non-ASCII?
I think there should be some better tool/method.
answered Jul 19 '11 at 21:15
enzotibenzotib
64k6135154
64k6135154
add a comment |
add a comment |
Here is a POSIX Awk function for encoding:
function encodeURIComponent(str, j, q) {
while (y++ < 125) z[sprintf("%c", y)] = y
while (y = substr(str, ++j, 1))
q = y ~ /[[:alnum:]_.!~*47()-]/ ? q y : q sprintf("%%%02X", z[y])
return q
}
Example
add a comment |
Here is a POSIX Awk function for encoding:
function encodeURIComponent(str, j, q) {
while (y++ < 125) z[sprintf("%c", y)] = y
while (y = substr(str, ++j, 1))
q = y ~ /[[:alnum:]_.!~*47()-]/ ? q y : q sprintf("%%%02X", z[y])
return q
}
Example
add a comment |
Here is a POSIX Awk function for encoding:
function encodeURIComponent(str, j, q) {
while (y++ < 125) z[sprintf("%c", y)] = y
while (y = substr(str, ++j, 1))
q = y ~ /[[:alnum:]_.!~*47()-]/ ? q y : q sprintf("%%%02X", z[y])
return q
}
Example
Here is a POSIX Awk function for encoding:
function encodeURIComponent(str, j, q) {
while (y++ < 125) z[sprintf("%c", y)] = y
while (y = substr(str, ++j, 1))
q = y ~ /[[:alnum:]_.!~*47()-]/ ? q y : q sprintf("%%%02X", z[y])
return q
}
Example
edited Dec 31 '16 at 5:23
answered Feb 29 '16 at 1:42
Steven PennySteven Penny
1
1
add a comment |
add a comment |
Similar to Stefano ansqer but in Python 3:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" æ
python -c "import urllib.parse, sys; print(urllib.parse.unquote(sys.argv[1]))" %C3%A6
To encode also slashes:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], ""))"
More info about the difference here.
add a comment |
Similar to Stefano ansqer but in Python 3:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" æ
python -c "import urllib.parse, sys; print(urllib.parse.unquote(sys.argv[1]))" %C3%A6
To encode also slashes:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], ""))"
More info about the difference here.
add a comment |
Similar to Stefano ansqer but in Python 3:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" æ
python -c "import urllib.parse, sys; print(urllib.parse.unquote(sys.argv[1]))" %C3%A6
To encode also slashes:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], ""))"
More info about the difference here.
Similar to Stefano ansqer but in Python 3:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" æ
python -c "import urllib.parse, sys; print(urllib.parse.unquote(sys.argv[1]))" %C3%A6
To encode also slashes:
python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], ""))"
More info about the difference here.
edited Jan 17 at 19:57
answered Jan 17 at 19:03
Pablo BianchiPablo Bianchi
2,77821533
2,77821533
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f53770%2fhow-can-i-encode-and-decode-percent-encoded-strings-on-the-command-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you want to incorporate different encodings too?
%E6ndr%FCkdoesn't look like (standard) UTF8 to me. Or it's just an example?– arrange
Jul 19 '11 at 21:13
@arrange Thanks for catching that. Apparently I chose the bad apple among search results for online converters.
– ændrük
Jul 19 '11 at 21:49
For file names, see: How to remove URI encoding in file names.
– kenorb
Feb 12 '15 at 17:54