我需要使用字典替换文件中的单词AAAA:
dictionary.txt
AXF1
ZCFA
ZCCC
字典约1500字。 我需要用AXF1替换AAAA然后我需要找到下一个AAAA并由ZCFA替换...... 知道我该怎么办?我发现的一切都是如何替换这样:
AAA1:AXF1
AAA2:ZCFA
etc...
答案 0 :(得分:1)
类似的东西:
# Read dictionary into memory
dictionary = [line.strip() for line in open('dictionary.txt')]
# Assuming a bit of a wrap around may be required depending on num. of AAAA's
from itertools import cycle
cyclic_dictionary = cycle(dictionary)
# Read main file
other_file = open('filename').read()
# Let's replace all the AAAA's
import re
re.sub('A{4}', lambda L: next(cyclic_dictionary), other_file, flags=re.MULTILINE)
答案 1 :(得分:1)
awk 'FNR == NR {list[c++] = $1; next}
{
while (sub("AAAA", list[n++])) {
n %= c
}
print
}' list.txt inputfile.txt
答案 2 :(得分:1)
这可能适合你(GNU sed):
cat <<\! >dictionary.txt
> AXF1
> ZCFA
> ZCCC
> !
cat <<\! >file.txt
> a
> b
> AAAA
> c
> AAAA
> d
> AAAA
> !
sed -e '/AAAA/{R dictionary.txt' -e ';d}' file.txt
a
b
AXF1
c
ZCFA
d
ZCCC