您有一个字典 Dictionary.txt 和一个输入文件 inFile.txt。字典会告诉您可能的翻译。unix shell中类似问题的解决方案:用字典替换似乎在这里硬编码了我无法完全理解的东西。您可以提出比字典更好的替换技术,但 AWK/Sed 脚本应该能够读取多个文件,在最简单的情况下,只有一个字典文件和一个 infile。
如何用 AWK 或 Sed 优雅地用字典替换?
例子
字典.txt
1 one 2 two 3 three four fyra five fem
inFile.txt
one 1 hello hallo 2 three hallo five five
来自命令的输出,我们追求类似的命令
awk/sed {} Dictionary.txt inFile.txt
one one hello hallo two three hallo fem fem
专门选择替换但一对一替换不起作用的 AWK 示例。
awk 'BEGIN { lvl[1] = "one" lvl[2] = "two" lvl[3] = "three" # TODO: this does not work # lvl[four] = "fyra" # lvl[five] = "fem" # lvl[one] = "one" # lvl["hello"] = "hello" # lvl[hallo] = "hallo" # lvl[three] = "three" } NR == FNR { evt[$1] = $2; next } { print $1, evt[$2], $3, $4, evt[$5], $6, $7, evt[$8], evt[$9] #TODO: this dos not work, eg. one-one mapping # print evt[$1], evt[$2], evt[$3], evt[$4], evt[$5], evt[$6], evt[$7], evt[$8], evt[$9] }' dictionary.txt infile.txt