开发者

Translate & to %20%26%20

I know I can translate upper to lower case letters by

echo 'linux' | tr "a-z" "开发者_开发技巧A-Z"

how would I translate or replace an occurrence of & with %20%26%20. Maybe something like this

echo '&' | tr "&" "%20%26%20"


sed -i 's/&/%20%26%20/g' inputfile

will edit the file in place.


you can just use the shell.

$ var="&test"
$ echo ${var//&/%20%26%20}
%20%26%20test


The reason that tr command does not work is that tr treats it's 2 arguments, not as arbitrary strings, but as lists. tr finds "&" in the string; it's the first element in the first argument; it gets replaced by the corresponding first element in the second argument.

From the man page:

When the -d option is not specified:

    o  Each input character found in the array  specified  by
       string1 is replaced by the character in the same rela-
       tive position in the array specified by string2.  When
       the array specified by string2 is shorter that the one
       specified by string1, the results are unspecified.

The sed and bash solutions offered are what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜