Decoding URLs inside AWK

One of the columns in my file is encoded in url, I have to decode this column and perform some operations based on the values ​​inside the column. Is there any way to decode this column in awk?

+4
source share
1 answer

You need to adapt it depending on the file format, but the basic principle is here (tested with 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 ! 

If you have gawk , you can wrap this in a function (credit brendanh in the 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; } 
+4
source

Source: https://habr.com/ru/post/1485209/


All Articles