【发布时间】:2023-03-28 09:14:01
【问题描述】:
我有两个文件(比如 file1 和 file2)。 file1 和 file2 中有字符串(字符串个数相等)。
我想在包含 XML 文件的目录(有多个子目录和 XML 文件)中搜索 file1 的内容,并将其替换为 file2 的内容。
import subprocess
import sys
import os
f_line = f.readlines()
g_line = g.readlines()
f=open("file1.txt")
g=open("file2.txt")
i = 0
for line in f_line:
if line.replace("\r\n", "") != g_line[i].replace("\r\n", "") :
print (line)
print(g_line[i])
cmd = "sed -i 's/" + line.replace("\r\n", "") + "/" + line[i].replace("\r\n","") + "/g' " + "`grep -l -R " + line.replace("\r\n", "") + " *.xml`"
print(cmd)
os.system(cmd)
i = i + 1
但我面临的问题是这样的。该脚本搜索文件和字符串并打印(打印(cmd)),但是当我将此脚本放置在目录中时,我在 CYGWIN 窗口中看到此错误“没有用于 sed 的输入文件”。
【问题讨论】:
-
这个问题没什么意义。您能否提供示例输入、输出以及您已经完成的工作?
-
@Derek file1 harry shoot raj file2 care 可怜的亲爱的,现在我想在某个目录(myfolder/project/)中搜索 file1 的字符串“harry”并替换为文件 2“care”的字符串和很快。我说清楚了吗?
-
@harry 你应该编辑你的问题以添加你的评论内容。
-
另外,你可能想要 sed grymoire.com/Unix/Sed.html