制作多个文件副本,用 bash 或 python 替换某些单词

Making several copies of a file replacing some word with bash or python

我有一些文件my_file。我有一个(长度为 n 的)单词列表,我想制作文件的 n 个副本,并将列表中的元素附加在文件末尾,并更改 'x' 在 my_file 中遇到的所有单词使用列表中的相应实体。

例如:如果我的文件是

----my_file.txt---
This is my x file
------------------

我的列表是 {a,b,c} 我想要三个文件:my_filea.txt、my_fileb.txt 和 my_filec.txt,每个文件中的字母 x 都改为 a, b,c,相应地。

我用bash或python做这个很方便。 有什么建议吗?

这是执行此操作的 python 脚本。

import re
data=open("myfile.txt","r").read()
listinput=['a','b','c']
for i in listinput:
    changed_data=re.sub("x file",i+" file",data)
    fp2=open("myfile"+i+".txt","w")
    fp2.write(changed_data)
    fp2.close()