我需要使用的字典文件中的替換詞AAAA:查找和替換使用字典中的單詞在linux
dictionary.txt
AXF1
ZCFA
ZCCC
詞典約1500字。 我需要用AXF1替換AAAA,然後我需要找到下一個AAAA並用ZCFA替換... 任何想法我該怎麼做?我發現一切是如何取代像這樣:
AAA1:AXF1
AAA2:ZCFA
etc...
我需要使用的字典文件中的替換詞AAAA:查找和替換使用字典中的單詞在linux
dictionary.txt
AXF1
ZCFA
ZCCC
詞典約1500字。 我需要用AXF1替換AAAA,然後我需要找到下一個AAAA並用ZCFA替換... 任何想法我該怎麼做?我發現一切是如何取代像這樣:
AAA1:AXF1
AAA2:ZCFA
etc...
喜歡的東西:
# 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)
awk 'FNR == NR {list[c++] = $1; next}
{
while (sub("AAAA", list[n++])) {
n %= c
}
print
}' list.txt inputfile.txt
這也適用。謝謝。 – user1209304
這可能會爲你工作(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
太棒了。它正在工作。這就是我一直在尋找 – user1209304