Shell 字符替换命令

Shell command for character replacement

我有这个字符串:"abcdef",我想用另一个字符串替换每个字符。我用 sed 做到了,但是发生了冲突,如果在替换的字符串上有另一个字符,例如 a 它再次用字符串替换 a。

例如:字符串 abcdef 将 a 替换为 arv 它将再次替换 a 字符。 有什么建议吗?

安装了 busybox 的环境 sh4 非常感谢

string='yhWEmUZltnEfmN8cZKayQkX8S8ejzgdUTiBhilDgLY2dLj0YgWgRJW3K7V3YVYwm6G9h79azWXiz3dK6pvfyuW1Jf5L093IcbrzFOkAf0jrhdunDrs7C7jR6eDlpfwLp9zUM8vxiK9kEzIfpjyKFcwO1VVTAKEZVm1x9YSxQkyq6DVcxCjjdtzJKW1WefxTf5s3THBpLMlpWzv23MrL9DZxRQvQgCOEWHgq0hI936vgansKWPW4sFNdlBlDFRsj1bsGvDD9VrROMjosrZzAYFe8Z5JAzsUVju7BiqbA9XslWjMRgdvAdTt87d3308dyujf98e'
decoded=$(echo -n "$string" | sed 's/yhWEm/a/g;s/UZltn/b/g;s/EfmN8/c/g;s/cZKay/d/g;s/QkX8S/e/g;s/8ejzg/f/g;s/dUTiB/g/g;s/hilDg/h/g;s/LY2dL/i/g;s/j0YgW/j/g;s/gRJW3/k/g;s/K7V3Y/l/g;s/VYwm6/m/g;s/G9h79/n/g;s/azWXi/o/g;s/z3dK6/p/g;s/pvfyu/q/g;s/W1Jf5/r/g;s/L093I/s/g;s/cbrzF/t/g;s/OkAf0/u/g;s/jrhdu/v/g;s/nDrs7/w/g;s/C7jR6/x/g;s/eDlpf/y/g;s/wLp9z/z/g;s/UM8vx/A/g;s/iK9kE/B/g;s/zIfpj/C/g;s/yKFcw/D/g;s/O1VVT/E/g;s/AKEZV/F/g;s/m1x9Y/G/g;s/SxQky/H/g;s/q6DVc/I/g;s/xCjjd/J/g;s/tzJKW/K/g;s/1Wefx/L/g;s/Tf5s3/M/g;s/THBpL/N/g;s/MlpWz/O/g;s/v23Mr/P/g;s/L9DZx/Q/g;s/RQvQg/R/g;s/CO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33/g;s/_/08dyu/g;s/-/jf98e/g;')
echo "Encrypt: $ecnoded"
second_decode=$(echo -n "$ecnoded" | sed 's/yhWEm/a/g;s/UZltn/b/g;s/EfmN8/c/g;s/cZKay/d/g;s/QkX8S/e/g;s/8ejzg/f/g;s/dUTiB/g/g;s/hilDg/h/g;s/LY2dL/i/g;s/j0YgW/j/g;s/gRJW3/k/g;s/K7V3Y/l/g;s/VYwm6/m/g;s/G9h79/n/g;s/azWXi/o/g;s/z3dK6/p/g;s/pvfyu/q/g;s/W1Jf5/r/g;s/L093I/s/g;s/cbrzF/t/g;s/OkAf0/u/g;s/jrhdu/v/g;s/nDrs7/w/g;s/C7jR6/x/g;s/eDlpf/y/g;s/wLp9z/z/g;s/UM8vx/A/g;s/iK9kE/B/g;s/zIfpj/C/g;s/yKFcw/D/g;s/O1VVT/E/g;s/AKEZV/F/g;s/m1x9Y/G/g;s/SxQky/H/g;s/q6DVc/I/g;s/xCjjd/J/g;s/tzJKW/K/g;s/1Wefx/L/g;s/Tf5s3/M/g;s/THBpL/N/g;s/MlpWz/O/g;s/v23Mr/P/g;s/L9DZx/Q/g;s/RQvQg/R/g;s/COEWH/S/g;s/gq0hI/T/g;s/936vg/U/g;s/ansKW/V/g;s/PW4sF/W/g;s/NdlBl/X/g;s/DFRsj/Y/g;s/1bsGv/Z/g;s/DD9Vr/0/g;s/ROMjo/1/g;s/srZzA/2/g;s/YFe8Z/3/g;s/5JAzs/4/g;s/UVju7/5/g;s/BiqbA/6/g;s/9XslW/7/g;s/jMRgd/8/g;s/vAdTt/9/g;s/87d33/=/g;s/08dyu/_/g;s/jf98e/-/g;')
echo "Second Decrypt: $second_decode"

您可以编写一个简单的 bash 脚本来完成这项工作(例如 replace.sh),方法是遍历(并替换)输入字符串的每个字符:

foo=$(</dev/stdin)
for (( i=0; i<${#foo};i++ )); do
  c=${foo:$i:1};
  case "$c" in
    "a")  c="bcd" ;;
    "b")  c="cde" ;;
  esac;
  echo -n $c;
done

并简单地调用它:

$ echo "abcd" | ./replace.sh
bcdcdecd

可惜你在 busybox 上没有 tclsh,它有一个内置的 string map 命令,这将是理想的。这是 awk 和 shell:

中的一个实现
#!/bin/sh

code () {
    codemap="a yhWEm b UZltn c EfmN8 d cZKay e QkX8S f 8ejzg g dUTiB h hilDg i LY2dL j j0YgW k gRJW3 l K7V3Y m VYwm6 n G9h79 o azWXi p z3dK6 q pvfyu r W1Jf5 s L093I t cbrzF u OkAf0 v jrhdu w nDrs7 x C7jR6 y eDlpf z wLp9z A UM8vx B iK9kE C zIfpj D yKFcw E O1VVT F AKEZV G m1x9Y H SxQky I q6DVc J xCjjd K tzJKW L 1Wefx M Tf5s3 N THBpL O MlpWz P v23Mr Q L9DZx R RQvQg S COEWH T gq0hI U 936vg V ansKW W PW4sF X NdlBl Y DFRsj Z 1bsGv 0 DD9Vr 1 ROMjo 2 srZzA 3 YFe8Z 4 5JAzs 5 UVju7 6 BiqbA 7 9XslW 8 jMRgd 9 vAdTt = 87d33 _ 08dyu - jf98e"

    echo "" | awk -v codemap="$codemap" -v method="" '
        BEGIN {
            n = split(codemap, a)
            for (i=1; i<n; i+=2) {
                _encoding[a[i]]   = a[i+1]
                _decoding[a[i+1]] = a[i]
            }

        }
        function decode(str) {
            decoded = ""
            while (str) {
                for (code in _decoding) {
                    if (substr(str, 0, length(code)) == code) {
                        str = substr(str, length(code)+1)
                        decoded = decoded _decoding[code]
                        break
                    }
                }
            }
            return(decoded)
        }
        function encode(str) {
            encoded = ""
            while (str) {
                for (code in _encoding) {
                    if (substr(str, 0, length(code)) == code) {
                        str = substr(str, length(code)+1)
                        encoded = encoded _encoding[code]
                        break
                    }
                }
            }
            return(encoded)
        }
        { 
            if (method == "encode") 
                print encode([=10=])
            else if (method == "decode")
                print decode([=10=])
        }
    '
}

encode () { code "" encode; }
decode () { code "" decode; }

string="yhWEmUZltnEfmN8cZKayQkX8S8ejzgdUTiBhilDgLY2dLj0YgWgRJW3K7V3YVYwm6G9h79azWXiz3dK6pvfyuW1Jf5L093IcbrzFOkAf0jrhdunDrs7C7jR6eDlpfwLp9zUM8vxiK9kEzIfpjyKFcwO1VVTAKEZVm1x9YSxQkyq6DVcxCjjdtzJKW1WefxTf5s3THBpLMlpWzv23MrL9DZxRQvQgCOEWHgq0hI936vgansKWPW4sFNdlBlDFRsj1bsGvDD9VrROMjosrZzAYFe8Z5JAzsUVju7BiqbA9XslWjMRgdvAdTt87d3308dyujf98e"

decoded=$(decode "$string")
echo "$decoded"
recoded=$(encode "$decoded")
[[ $string == "$recoded" ]] && echo success || echo fail