批量查找文件中的文本并替换为其他文本

Batch find text in file and replace with other text

我需要使用 Batch 来检查文件 Directory.twml 以查看它是否包含文件 blocked.twml 中的任何单词,以及它是否替换为 [Blocked]

这是两个文件的示例:

Directory.twml

11:38:38.90 [System] Twml Has joined the chat.
11:38:41.17 [User]   Twml says: line one
11:38:42.96 [User]   Twml says: line bad two
11:38:46.27 [User]   Twml says: line three
11:38:50.16 [User]   Twml says: you get the idea here
11:38:52.35 [System] Twml Has logged off.

Blocked.twml

word1
word2
word3
bad
word5
word6

我想Directory.twml说的是

11:38:38.90 [System] Twml Has joined the chat.
11:38:41.17 [User]   Twml says: line one
11:38:42.96 [User]   Twml says: line [Blocked] two
11:38:46.27 [User]   Twml says: line three
11:38:50.16 [User]   Twml says: you get the idea here
11:38:52.35 [System] Twml Has logged off.

我已经可以使用 Findstr 查看文件并查看文本是否存在,但就我所知,我不需要检查设置的单词,而是检查文件中的单词列表 Blocked.twml

findstr /i "bad" <"Directory.twml" 1>nul

我也可以从文件中删除单词,但我想替换它而不仅仅是删除

findstr /i /v /c:"%text%" Directory.twml > "Directory.twmll" 2>nul 
del Directory.twml /s /a >nul
copy Directory.twmll Directory.twml >nul
attrib +h Directory.twml
del Directory.twmll /s /a >nul

但这又是一组文本,不是从文件中的列表中查找的文本

如果 Directory.twml 包含 Blocked.twml 中的任何内容,请替换为 [Blocked],但我不知道该怎么做

=========编辑===========

这是解决方案:

(
for /f "delims=" %%A in (Directory.twml) do (
set "line=%%A"
for /f "delims=" %%B in (blocked.twml) do set "line=!line: %%B = [Blocked] !"
echo !line!
)
)>Directory.new

它的输出对我来说是这样的

13:22:14.16 [User]   twml says: this is a test
13:22:20.37 [User]   twml says: this is a [Blocked] word test

逐行阅读Directory.twml。对于每一行,阅读 blocked.twml 并将每个单词替换为字符串 [Blocked]。回显更改后的行。将整个输出重定向到新文件:

@echo off 
SETLOCAL ENABLEDELAYEDEXPANSION
(
  for /f "delims=" %%A in (Directory.twml) do (
    set "line=%%A"
    for /f "delims=" %%B in (blocked.twml) do set "line=!line:%%B=[Blocked]!"
    echo !line!
  )
)>Directory.new

我会留给你将新文件重命名为原始名称。

注意:类似 abadad 的内容将更改为 a[Blocked]ad。您可以将 set "line=!line:%%B=[Blocked]!" 更改为 set "line=!line: %%B = [Blocked] !" 以捕获单词边界,但 This is bad, I think. 不会更改。

注意:单个!将被删除。如果一行中有多个 !,它们之间的文本将消失。批处理并不是做这些事情的好选择...

  1. 只需 1 个命令行,使用 msr.exe 替换 Directory.twml 中的文本 如果它是可替换的:

for /f "tokens=*" %a in (Blocked.twml) do @msr -p Directory.twml -i -x "%a" -o "[Blocked]" -R

  • 如果 Blocked.twml 有空白行并转义它们,则更安全:

    for /f "tokens=*" %a in ('msr -p Blocked.twml -t "\w+" -PAC') do @msr -p Directory.twml -i -x "%a" -o "[Blocked]" -R 结果如:

2.If Directory.twml 不应该替换,根据您的目的使用以下方法:

  • 您可以将 Directory.twml 复制到 tmp 文件并在上面的命令中使用该 tmp 文件。
  • 如果你只想显示被替换的行,而不替换文件:

    • 使用-O只显示matched/replaced命令结果:

    for /f "tokens=*" %a in (Blocked.twml) do @msr -p Directory.twml -x "%a" -o "[Blocked]" -O

    • 使用-O -P -A显示纯替换结果:(如果不想要颜色,请添加-C

    for /f "tokens=*" %a in (Blocked.twml) do @msr -p Directory.twml -x "%a" -o "[Blocked]" -OPA

    结果如下: msr.exe / msr.gcc* 是一个单一的可移植 exe 工具,大约 1.6MB,没有依赖项,具有跨平台版本,用于查找和替换带有颜色和摘要信息的文件文本以及备份支持等。请参阅我的打开项目https://github.com/qualiu/msr (tools directory), docs like usage, performance comparison with findstr and grep; built-in doc like: https://qualiu.github.io/msr/usage-by-running/msr-Windows.html