2019年 · Linux

linux shell实现 URL 编码/解码方法

root@localhost:/data# cat 111.txt
aaa aaaaa
bbbbbbb
ccc,ccc
dd,dddd
#文件多行转换成一行
root@localhost:/data# cat 111.txt |sed ‘:a;N;$!ba;s/\n/\_/g’ -|sed ‘s/[[:space:]]/\_/g;s/\,/\_/g’
aaaaaaaa_bbbbbbb_cccccc_dddddd
(1)编码的两种方法:
root@localhost:/data# cat 111.txt |sed ‘:a;N;$!ba;s/\n/\_/g’ -|sed ‘s/[[:space:]]/\_/g’ |sed ‘s/\,//g’|tr -d ‘\n’ | xxd -plain | sed ‘s/\(..\)/%\1/g’
%61%61%61%61%61%61%61%61%5f%62%62%62%62%62%62%62%5f%63%63%63%63%63%63%5f%64%64%64%64%64%64
root@localhost:/data# cat 111.txt |sed ‘:a;N;$!ba;s/\n/\_/g’ -|sed ‘s/[[:space:]]/\_/g’ |sed ‘s/\,//g’|tr -d ‘\n’ |od -An -tx1|tr ‘ ‘ %
%61%61%61%61%61%61%61%61%5f%62%62%62%62%62%62%62
%5f%63%63%63%63%63%63%5f%64%64%64%64%64%64

(2)解码的两个步骤:
root@localhost:/data# url=”%61%61%61%61%61%61%61%61%5f%62%62%62%62%62%62%62%5f%63%63%63%63%63%63%5f%64%64%64%64%64%64″
root@localhost:/data# printf $(echo -n $url |sed ‘s/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g’)”\n”
aaaaaaaa_bbbbbbb_cccccc_dddddd