简体   繁体   English

如何在批处理脚本中查找和替换文件内容

[英]How to Find and Replace file content in batch script

For example I have the file sample.txt. 例如,我有sample.txt文件。 This file contains: 该文件包含:

1111101                       
2222203                       
3333303                       
44444A1                       
55555A1                       
66666A1 

Now, I want to replace user defined specific pattern. 现在,我要替换用户定义的特定模式。 For example I have other file where use defines what he want to replace with. 例如,我有另一个文件,其中use定义了他要替换的内容。 Example the file name is replace.txt. 例如,文件名是replace.txt。 This file contains 2 Columns, first column for the pattern and the 2nd column for the text to be replace. 该文件包含2列,第一列用于模式,第二列用于要替换的文本。

Example: 例:

replace.txt replace.txt

2222203         2222203ADD
55555A1         55555A1SUB

Now, when the batch file has been executed, I would like the file sample.txt to have a contents like this: 现在,执行批处理文件后,我希望文件sample.txt具有如下内容:

1111101                       
2222203ADD                       
3333303                       
44444A1                       
55555A1SUB                       
66666A1 

Also is it possible to have a "space" as part of the text to be replace(column 2? 还有可能在要替换的文本中包含一个“空格”(第2列?

You may use FindRepl.bat program that is a Batch-JScript hybrid application that perform these replacements in a very efficient way via regular expressions ; 您可以使用作为Batch-JScript混合应用程序的FindRepl.bat程序,该程序通过正则表达式以非常有效的方式执行这些替换; it uses JScript language that is standard in all Windows versions from XP on. 它使用XP以后所有Windows版本中标准的JScript语言。 In the basic use of FindRepl.bat you redirect the input file to it and place two strings as parameters, a "search" string and a "replacement" string. 在FindRepl.bat的基本用法中,您将输入文件重定向到该文件,并放置两个字符串作为参数,即“搜索”字符串和“替换”字符串。 For example: 例如:

< sample.txt FindRepl.bat "2222203" "2222203ADD"

Previous command will replace all 2222203 strings in the file by 2222203ADD . 前一个命令将取代所有2222203在文件中的字符串2222203ADD In order to perform the replacement of several strings, you may include several alternatives in both the search and replacement strings separated by a pipe character (this is called alternation ), and include the /A switch to select this feature; 为了执行多个字符串的替换,您可以在搜索字符串和替换字符串中都包含多个替代项,并用竖线字符分隔这两个字符串(这称为alternation ),并包括/ A开关以选择此功能; for example: 例如:

< sample.txt FindRepl.bat "2222203|55555A1" /A "2222203ADD|55555A1SUB"

If you want to define the set of replacements in a separated file, you just need to load the strings from the file, assemble the alternations in two variables and use they in FindRepl preceded by an equal-sign to indicate that they are variables, not literal strings. 如果要在单独的文件中定义替换集,则只需要从文件中加载字符串,将替换组合成两个变量,并在FindRepl中使用它们,并在等号之前使用等号表示它们是变量,而不是文字字符串。 If you want that the strings may have spaces, then you must use a different character to separate the search and replace parts in the file. 如果希望字符串中可以有空格,则必须使用其他字符分隔搜索并替换文件中的部分。 For example, if you use a colon in replace.txt file this way: 例如,如果您以这种方式在replace.txt文件中使用冒号:

2222203:2222203 ADD
55555A1:55555A1 SUB

Then the Batch file below solve your problem: 然后,下面的批处理文件解决了您的问题:

@echo off
setlocal EnableDelayedExpansion
set "search="
set "replace="
for /F "tokens=1,2 delims=:" %%a in (replace.txt) do (
   set "search=!search!|%%a"
   set "replace=!replace!|%%b"
)
set "search=!search:~1!"
set "replace=!replace:~1!"
< sample.txt FindRepl.bat =search /A =replace

You may download FindRepl.bat and review an explanation of its use from this site ; 您可以从该站点下载FindRepl.bat并查看其用法说明; you must place it in the same folder of previous program or, better yet, in a folder included in PATH variable. 您必须将其放置在先前程序的同一文件夹中,或者最好放置在PATH变量中包含的文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Windows批处理脚本在“,”中查找和替换文本文件中的“=”? - How to find and replace “=” in a text file with “,” using windows batch script? 批处理脚本查找和替换 - Batch script find and replace 查找直到该行,然后用另一个文本文件Windows批处理脚本替换 - Find until that line and replace with another text file windows batch script 在批处理文件上运行查找/替换Powershell脚本后,不再执行 - Batch file no longer executes after running find/replace Powershell script on it 批处理文件-查找文件并替换 - Batch File - Find file and Replace 如何创建批处理脚本以查找和替换多个文档中的页面 - How to create a batch script to find and replace pages within multiple documents 内部循环查找和替换[批处理脚本] - Find and Replace inside for loop [batch script] 在文本文件中用CRLF替换CR的批处理脚本 - Batch script to Replace CR by CRLF in in text file 使用批处理脚本在文本文件中查找和替换字符串的算法,有效,但随机停止 - Find and replace algorithm for string in text file using batch script, works, but stopping at random Line 用于替换文件中的环境变量的Windows批处理脚本 - Windows Batch Script to Replace Environment Variables in a File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM