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

时间:2017-10-11 14:52:33

标签: batch-file findstr

我需要使用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中的单词列表< / p>

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

2 个答案:

答案 0 :(得分:0)

逐行读取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 :(得分:0)

  1. 只需1个命令行,使用msr.exe替换Directory.twml 中的文字,如果它可以替换
  2. 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 结果如: Replace-file-method

    2.如果Directory.twml不应被替换,请根据您的目的使用以下方法:

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

      • 使用-O显示匹配/替换的命令结果:

      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

      结果如: just-replace-text-not-file msr.exe / msr.gcc*是一个单独的可移植exe工具,大约1.6MB,没有依赖关系和跨平台版本,用颜色和摘要信息以及备份支持等查找和替换文件文本。请参阅我打开的项目https://github.com/qualiu/msrtools目录),使用文档,与 findstr grep 的效果比较;内置文档,例如:https://qualiu.github.io/msr/usage-by-running/msr-Windows.html

相关问题