替代基于字典的文字 [英] replace text based on a dictionary
问题描述
我需要做类似这样的事情后(但与一捻)。这就是为什么我问。
I need to do something similar to this post (but with a twist). That is why I am asking.
我有一本字典(dict.txt)。据空格分隔,它读起来就像这样:
I have a dictionary(dict.txt). It is space separated and it reads like this:
V7动量
B6广达
...
(第一列是键,第二列是值,在一定意义上)
(the first column is key and the second column is value, in a sense)
我有一个用户文件(user.txt),它包含了键(V7,B6等)的发生。捻是键不是在自己的列(所以在上面的帖子方法不适用)。
I have a user file (user.txt), it contains the occurrences of the keys (V7, B6 etc). The twist is that keys are not in its own column (so the method in the above post does not apply).
用户文件(user.txt)可以查看字符流。我只是想替换键(例如,V7),无论它们是由空间界或其它字符界的值(动量)的所有出现在字典中抬起头来。
The user file (user.txt) can be view as a stream of characters. I just want to replace all occurrences of the keys (e.g., V7), regardless they are bounded by space or bounded by other character by the value (Momentum) looked up from the dictionary.
例如:
我们必须V7作为输入 - >应更改为 - >我们有动力,因为输入
"We have V7 as input" --> should change to --> "We have Momentum as input"
我们有somethingV7_as输入 - >应更改为 - >我们有somethingMomentum_as输入
"We have somethingV7_as input" -->should change to --> "We have somethingMomentum_as input"
推荐答案
用法:的awk -f foo.awk dict.dat USER.DAT
结果
http://www.gnu.org/software/gawk/manual /html_node/String-Functions.html 结果
http://www.gnu.org/software/gawk/manual/html_node /Arrays.html
NR == FNR {
rep[$1] = $2
next
}
{
for (key in rep)
gsub(key, rep[key])
print
}
这篇关于替代基于字典的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!