linux - linux shell的音译脚本

标签 linux shell sed tr

我有多个包含字母表文本的 .txt 文件;我想将文本音译成其他字母表; alphabet1 的某些字符与 alphabet2 的字符是 1:1(即 a 变为 e),而其他字符是 1:2(即 x 变为ch)。

我想使用适用于 Linux shell 的简单脚本来执行此操作。

使用 trsed 我可以转换 1:1 字符:

sed -f y/abcdefghijklmnopqrstuvwxyz/nopqrstuvwxyzabcdefghijklm/

a 将变为 nb 将变为 o 等等(我认为是凯撒密码)

但是我该如何处理 1:2 字符呢?

最佳答案

使用 Awk:

#!/usr/bin/awk -f
BEGIN {
    FS = OFS = ""
    table["a"] = "e"
    table["x"] = "ch"
    # and so on...
}
{
    for (i = 1; i <= NF; ++i) {
        if ($i in table) {
            $i = table[$i]
        }
    }
}
1

用法:

awk -f script.awk file

测试:

# echo "the quick brown fox jumps over the lazy dog" | awk -f script.awk
the quick brown foch jumps over the lezy dog

关于linux - linux shell的音译脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25338470/

相关文章:

bash - 带有 mysqldump 的 shell 脚本

linux - 这个linux命令的结果是什么

python - 将脚本从 Linux 更改为 Python - 自动化

linux - 在 linux 下,如何以编程方式可靠地获取与具有给定 pid 的进程对应的可执行路径?

linux - 使用 apt-get install 配置时链接 boost_system?

linux - 这个命令到底在做什么?

linux - 如何为此代码编写 if 记录的缩写版本?

regex - 用于更新 ssl.conf 中 SSL 证书文件路径的 Bash 脚本(RegEx 单词与 # 匹配)

bash - 使用sed从字符串中删除子字符串

regex - 使用 sed 扩展环境变量