From: superpollo on 22 Jan 2010 15:54 Stephane CHAZELAS ha scritto: > 2010-01-22, 16:12(+01), superpollo: >> is there a smarter way to do percent decoding (and encoding) other than >> a mere succession of sed substitutions? > > $ perl -MURI::Escape -le 'print uri_escape "3% total"' > 3%25%20total > or: $ python -c 'import urllib ; print urllib.quote("3% total")' for that matter... i dont know, i was looking for a more "bash-ey" way ;-) bye
From: Ben Bacarisse on 22 Jan 2010 18:40 superpollo <utente(a)esempio.net> writes: > superpollo ha scritto: <snip> >>>>>> is there a smarter way to do percent decoding (and encoding) >>>>>> other than >>>>>> a mere succession of sed substitutions? <snip> > ok that's how i do it: > > if [ "$REQUEST_METHOD" == "GET" ] > then > DATA=$QUERY_STRING > elif [ "$REQUEST_METHOD" == "POST" ] > then > DATA=$(head --bytes="$CONTENT_LENGTH") > NAME=$(echo "$DATA" | sed -e "s/^\(.*\)=.*$/\1/g") > ENCVALUE=$(echo "$DATA" | sed -e "s/^.*=\(.*\)$/\1/g") > PRINTFVALUE=$(echo "$ENCVALUE" | sed -e "s/+/ /g" \ > -e "s/%\([0-9A-F][0-9A-F]\)/\\\\x\1/g") > VALUE=$(printf "$PRINTFVALUE") > > any suggestion for improvementt? I can see a couple of problems: The printf should be printf "%b" "$PRINTFVALUE" or % characters will be taken as formats. URLs with an un-encoded \ in it are likely to go wrong. -- Ben.
From: bsh on 25 Jan 2010 21:49 Hallvard B Furuseth <h.b.furus...(a)usit.uio.no> wrote: > superpollo writes: > > ... > perl -wple 's/([^\w\s])/sprintf("%%%02X", ord($1))/eg' Perl(1) is very powerful and acceptably efficient, but do you know how much overhead there is in even _invoking_ the new perl(1) process!? From the Kornshell FAQ at http://kornshell.com/doc/faq.html: Q25. How can I convert %XX values to ascii? A25. You can convert this to a sequence of ANSI C strings and then eval that string, for example suppose the variable 'foo' contains %XX strings, then eval print -r -- "\$'${foo//'%'@(??)/'\x\1"'\$'"}'" will print out the string in ascii. The above uses ksh version 1993 or newer features. =Brian
From: Stephane CHAZELAS on 26 Jan 2010 14:36 2010-01-25, 18:49(-08), bsh: > Hallvard B Furuseth <h.b.furus...(a)usit.uio.no> wrote: >> superpollo writes: >> > ... >> perl -wple 's/([^\w\s])/sprintf("%%%02X", ord($1))/eg' > > Perl(1) is very powerful and acceptably efficient, but do you know how > much overhead there is in even _invoking_ the new perl(1) process!? > > From the Kornshell FAQ at http://kornshell.com/doc/faq.html: [...] $ ls -lL =ksh =perl -rwxr-xr-x 1 root root 1240636 Jun 4 2009 /usr/bin/ksh* -rwxr-xr-x 2 root root 1232892 Jan 18 08:00 /usr/bin/perl* $ time zsh -c 'repeat 1000 ksh -c :' zsh -c 'repeat 1000 ksh -c :' 1.62s user 2.50s system 99% cpu 4.136 total $ time zsh -c 'perl -e ""' zsh -c 'perl -e ""' 0.00s user 0.03s system 97% cpu 0.037 total ~$ time zsh -c 'repeat 1000 perl -e ""' zsh -c 'repeat 1000 perl -e ""' 1.52s user 2.60s system 98% cpu 4.162 total $ ksh -c 'ps -o rss,comm;:' RSS COMMAND 2660 zsh 1392 ksh 772 ps $ perl -le 'exec "ps", "-o", "rss,comm" unless fork; wait' RSS COMMAND 2660 zsh 1388 perl 772 ps Impressive how similar, eh? The overhead of invoking a command is only critical if it's involved in a loop. And IMO, using loops in shells is a sign of bad practice, just as trying not to invoke commands which is the main purpose of a shell. -- St�phane
From: Janis Papanagnou on 26 Jan 2010 15:26 Stephane CHAZELAS wrote: > 2010-01-25, 18:49(-08), bsh: >> Hallvard B Furuseth <h.b.furus...(a)usit.uio.no> wrote: >>> superpollo writes: >>>> ... >>> perl -wple 's/([^\w\s])/sprintf("%%%02X", ord($1))/eg' >> Perl(1) is very powerful and acceptably efficient, but do you know how >> much overhead there is in even _invoking_ the new perl(1) process!? >> >> From the Kornshell FAQ at http://kornshell.com/doc/faq.html: > [...] > [ runtime comparison of ksh/perl invocation snipped ] > > Impressive how similar, eh? Well, not that astonishing. And surely besides the poster's point. > > The overhead of invoking a command is only critical if it's > involved in a loop. And IMO, using loops in shells is a sign of > bad practice, just as trying not to invoke commands which is the > main purpose of a shell. > Probably all true. Though the point was to not invoke an extra process if you can do it within the _same_ shell session you already are. If one wants to have that function in a loop, using the shell builtin version is certainly cheaper than invoking perl each time. Completely switching to perl instead of staying in shell might compensate that drawback but might not be an option if the user is not proficient in perl. One can also apply other techniques, like a perl co-process for that conversion, but as long as you can do that natively in shell, why bother. Janis
First
|
Prev
|
Pages: 1 2 3 4 Prev: Want to obtain single line output based on a file. Next: Simple regex question |