我需要在Bash脚本中编写函数来"加密"文本。我决定使用AWK来实现这个功能,但我有一个问题。
不幸的是,我不知道如何在字符串中找到字符的位置:
示例字符串:
abcdefghijklmnopqrstuvwxyz=,;.:-_+*?()!$&<>|ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
结果:
input a -> output 1
input b -> output 2
...
input = -> output 27
...
input 8 -> output 79
input 9 -> output 80
...
输入将始终只有一个字符。
你有什么想法吗?我该怎么做(最好是AWK)?
谢谢,Gregory
您可以使用index
函数在字符串中查找子字符串的索引。
使用
index(string, substring)
例如
$ awk '{print index("hello world", "h") }'
1
$ awk '{print index("hello world", "e") }'
2