Using awk printf to urldecode text

后端 未结 5 1549
一向
一向 2021-01-05 03:23

I\'m using awk to urldecode some text.

If I code the string into the printf statement like printf \"%s\", \"\\x3D\" it correct

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 03:54

    To start with, I'm aware this is an old question, but none of the answers worked for me (restricted to busybox awk)

    Two options. To parse stdin:

    awk '{for (y=0;y<127;y++) if (y!=37) gsub(sprintf("%%%02x|%%%02X",y,y), y==38 ? "\\&" : sprintf("%c", y));gsub(/%25/, "%");print}'
    

    To take a command line parameter:

    awk 'BEGIN {for (y=0;y<127;y++) if (y!=37) gsub(sprintf("%%%02x|%%%02X",y,y), y==38 ? "\\&" : sprintf("%c", y), ARGV[1]);gsub(/%25/, "%", ARGV[1]);print ARGV[1]}' parameter
    

    Have to do %25 last because otherwise strings like %253D get double-parsed, which shouldn't happen.

    The inline check for y==38 is because gsub treats & as a special character unless you backslash it.

提交回复
热议问题