perl -pe 's/%25([0-9A-F][0-9A-F])/chr hex $1/ge' myfile.txt
output
],"actualPage":1,"rowPerPage":50}]
Domanda
I have a file which contains long lines made of text mixed with encoded character.
%255D%252C%2522actualPage%2522%253A1%252C%2522rowPerPage%2522%253A50%257D%255D
Each encoder character is %25xx
where xx
is the hexa value of the ascii char (ex. %2540 = @
)
I tried the folowing but w/o success
perl -pe 's/%25([0-9A-F](0-9A-F])/\x$1/' myfile.txt
perl -pe 's/%25([0-9A-F](0-9A-F])/chr($1)/' myfile.txt
Do you have any clue for me ?
TIA, Peyre
Soluzione 3
perl -pe 's/%25([0-9A-F][0-9A-F])/chr hex $1/ge' myfile.txt
output
],"actualPage":1,"rowPerPage":50}]
Altri suggerimenti
Perhaps what you want is URI::Encode
. It is a better idea to use a module for this than a regex.
perl -MURI::Encode -nle'$u=URI::Encode->new(); print $u->decode($u->decode($_));'
Output is this for your input string:
],"actualPage":1,"rowPerPage":50}]
As you'll notice, the string had to be decoded twice, because it had been encoded twice (%25
is apparently the percent sign %
). The interim output was
%5D%2C%22actualPage%22%3A1%2C%22rowPerPage%22%3A50%7D%5D
perl -MURI::Escape -ne'print uri_unescape(uri_unescape($_))'