这个问题在这里已经有了答案:
Using awk printf to urldecode text
(5 个回答)
8年前关闭。
我的文件中的一列是 url 编码的,我必须解码该列并需要根据列内的值执行一些操作。有什么办法可以在 awk 中解码该列吗?
最佳答案
您必须根据您的文件格式对其进行调整,但基本原理在这里(使用 GNU Awk 3.1.7 测试):
sh$ echo 'Hello%2C%20world%20%21' | awk '
{
for (i = 0x20; i < 0x40; ++i) {
repl = sprintf("%c", i);
if ((repl == "&") || (repl == "\\"))
repl = "\\" repl;
gsub(sprintf("%%%02X", i), repl);
gsub(sprintf("%%%02x", i), repl);
}
print
}
'
Hello, world !
如果您有
gawk
,您可以将其包装在一个函数中(归功于 brendanh in a comment below ):function urlDecode(url) {
for (i = 0x20; i < 0x40; ++i) {
repl = sprintf("%c", i);
if ((repl == "&") || (repl == "\\")) {
repl = "\\" repl;
}
url = gensub(sprintf("%%%02X", i), repl, "g", url);
url = gensub(sprintf("%%%02x", i), repl, "g", url);
}
return url;
}
关于awk - AWK 中的 URL 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17002841/