如何将字符串的每个字母移动一定数量的字母?

Eup*_*ium 12 string bash

如何在不使用硬编码字典的情况下,在bash中向下或向上移动字符串的每个字母?

pax*_*blo 18

你的意思是像ROT13:

pax$ echo 'hello there' | tr '[a-z]' '[n-za-m]'
uryyb gurer

pax$ echo 'hello there' | tr '[a-z]' '[n-za-m]' | tr '[a-z]' '[n-za-m]'
hello there
Run Code Online (Sandbox Code Playgroud)

对于要提供任意旋转(0到26)的更通用的解决方案,您可以使用:

#!/usr/bin/bash

dual=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
phrase='hello there'
rotat=13
newphrase=$(echo $phrase | tr "${dual:0:26}" "${dual:${rotat}:26}")
echo ${newphrase}
Run Code Online (Sandbox Code Playgroud)


gma*_*gno 8

如果你还想旋转大写字母,你可以使用这样的东西:

cat data.txt | tr '[a-z]' '[n-za-m]' | tr '[A-Z]' '[N-ZA-M]'
Run Code Online (Sandbox Code Playgroud)

其中 data.txt 包含您想要旋转的任何内容。


Ign*_*ams 7

$ alpha=abcdefghijklmnopqrstuvwxyz
$ rot=3
$ sed "y/${alpha}/${alpha:$rot}${alpha::$rot}/" <<< 'foobar'
irredu
Run Code Online (Sandbox Code Playgroud)