Using awk printf to urldecode text

后端 未结 5 1529
一向
一向 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:53

    GNU awk

    #!/usr/bin/awk -fn
    @include "ord"
    BEGIN {
      RS = "%.."
    }
    {
      printf RT ? $0 chr("0x" substr(RT, 2)) : $0
    }
    

    Or

    #!/bin/sh
    awk -niord '{printf RT?$0chr("0x"substr(RT,2)):$0}' RS=%..
    

    Decoding URL encoding (percent encoding)

    0 讨论(0)
  • 2021-01-05 03:53

    This relies on gnu awk's extension of the split function, but this works:

    gawk '{ numElems = split($0, arr, /%../, seps);
            outStr = ""
            for (i = 1; i <= numElems - 1; i++) {
                outStr = outStr arr[i]
                outStr = outStr sprintf("%c", strtonum("0x" substr(seps[i],2)))
            }
            outStr = outStr arr[i]
            print outStr
          }'
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-05 04:05

    Since you're using ash and Perl isn't available, I'm assuming that you may not have gawk.

    For me, using gawk or busybox awk, your second example works the same as the first (I get "=" from both) unless I use the --posix option (in which case I get "x3D" for both).

    If I use --non-decimal-data or --traditional with gawk I get "=".

    What version of AWK are you using (awk, nawk, gawk, busybox - and version number)?

    Edit:

    You can coerce the variable's string value into a numeric one by adding zero:

    ~/busybox/awk 'BEGIN { string="3D"; pre="0x"; hex=pre string; printf "%c", hex+0}'
    
    0 讨论(0)
  • 2021-01-05 04:09

    I don't know how you do this in awk, but it's trivial in perl:

    echo "http://example.com/?q=foo%3Dbar" | 
        perl -pe 's/\+/ /g; s/%([0-9a-f]{2})/chr(hex($1))/eig'
    
    0 讨论(0)
提交回复
热议问题