- 论坛徽章:
- 0
|
回复 #1 daniel_kohler 的帖子
自问自答:
本来是很简单的一句,没想到要用这么长的语句替换。
- echo "adsfkjladsjf%E0%E0" | awk '
- BEGIN {
- hextab="0123456789ABCDEF"
- for ( i=1; i<=255; ++i ) ord [i] = sprintf("%c",i);
- }
- {
- decoded = ""
- for ( i=1; i<=length ($0); ++i ) {
- c = substr ($0, i, 1)
- if ( c ~ /[a-zA-Z0-9.-]/ ) {
- decoded = decoded c # safe character
- }
- else if ( c == " " ) {
- decoded = decoded "+" # special handling
- } else if ( c == "%" ) {
- hi= substr($0,i+1,1);
- low=substr($0,i+2,1);
- i++;i++
- decoded = decoded ord[(index(hextab,hi)-1)*16+index(hextab,low)-1]
- }
- }
- }
- END{print decoded}
复制代码
[ 本帖最后由 daniel_kohler 于 2010-1-22 13:13 编辑 ] |
|