如何使用 Windows 命令行环境查找和替换文件中的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/60034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you find and replace text in a file using the Windows command-line environment?
提问by Ray
I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. "FOO") with another (ex. "BAR"). What is the simplest way to do that? Any built in functions?
我正在使用 Windows 命令行环境编写批处理文件脚本,并希望将文件中某些文本(例如“FOO”)的每次出现更改为另一个(例如“BAR”)。最简单的方法是什么?有内置函数吗?
采纳答案by Rachel
A lot of the answers here helped point me in the right direction, however none were suitable for me, so I am posting my solution.
这里的很多答案帮助我指明了正确的方向,但是没有一个适合我,所以我发布了我的解决方案。
I have Windows 7, which comes with PowerShell built-in. Here is the script I used to find/replace all instances of text in a file:
我有 Windows 7,它内置了 PowerShell。这是我用来查找/替换文件中所有文本实例的脚本:
powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt"
To explain it:
解释一下:
powershell
starts up powershell.exe, which is included in Windows 7-Command "... "
is a command line arg for powershell.exe containing the command to run(gc myFile.txt)
reads the content ofmyFile.txt
(gc
is short for theGet-Content
command)-replace 'foo', 'bar'
simply runs the replace command to replacefoo
withbar
| Out-File myFile.txt
pipes the output to the filemyFile.txt
-encoding ASCII
prevents transcribing the output file to unicode, as the comments point out
powershell
启动 Windows 7 中包含的 powershell.exe-Command "... "
是 powershell.exe 的命令行参数,包含要运行的命令(gc myFile.txt)
读取myFile.txt
(命令的gc
缩写Get-Content
)的内容-replace 'foo', 'bar'
只需运行replace命令替换foo
用bar
| Out-File myFile.txt
将输出通过管道传输到文件myFile.txt
-encoding ASCII
正如注释所指出的,防止将输出文件转录为 unicode
Powershell.exe should be part of your PATH statement already, but if not you can add it. The location of it on my machine is C:\WINDOWS\system32\WindowsPowerShell\v1.0
Powershell.exe 应该已经是您的 PATH 语句的一部分,但如果不是,您可以添加它。它在我机器上的位置是C:\WINDOWS\system32\WindowsPowerShell\v1.0
回答by Mike Schall
If you are on Windows version that supports .Net 2.0, I would replace your shell. PowerShellgives you the full power of .Net from the command line. There are many commandlets built in as well. The example below will solve your question. I'm using the full names of the commands, there are shorter aliases, but this gives you something to Google for.
如果您使用的是支持 .Net 2.0 的 Windows 版本,我会更换您的外壳。 PowerShell从命令行为您提供 .Net 的全部功能。还内置了许多命令行开关。下面的例子将解决你的问题。我使用的是命令的全名,有更短的别名,但这为 Google 提供了一些东西。
(Get-Content test.txt) | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt
回答by VonC
Just used FART("Find And Replace Text" command line utility):
excellent little freeware for text replacement within a large set of files.
刚使用FART(“ ˚FIND甲ND - [RE放置ŤEXT”命令行实用程序):
优良小免费软件为一大组文件中的文本替换。
The setup files are on SourceForge.
安装文件位于 SourceForge 上。
Usage example:
用法示例:
fart.exe -p -r -c -- C:\tools\perl-5.8.9\* @@APP_DIR@@ C:\tools
will preview the replacements to do recursively in the files of this Perl distribution.
将预览在此 Perl 发行版的文件中递归执行的替换。
Only problem: the FART website icon isn't exactly tasteful, refined or elegant ;)
唯一的问题:FART 网站图标不够雅致、精致或优雅;)
Update 2017 (7 years later) jagbpoints out in the commentsto the 2011 article "FARTing the Easy Way – Find And Replace Text" from Mikail Tun?
2017 年更新(7 年后)jagb在Mikail Tun的 2011 年文章“ FARTing the Easy Way – Find And Replace Text”的评论中指出?
回答by Bill Richardson
Replace - Replace a substring using string substitution Description: To replace a substring with another string use the string substitution feature. The example shown here replaces all occurrences "teh" misspellings with "the" in the string variable str.
替换 - 使用字符串替换替换子字符串 说明:要使用另一个字符串替换子字符串,请使用字符串替换功能。此处显示的示例将字符串变量 str 中所有出现的“teh”拼写错误替换为“the”。
set str=teh cat in teh hat
echo.%str%
set str=%str:teh=the%
echo.%str%
Script Output:
脚本输出:
teh cat in teh hat
the cat in the hat
ref: http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
参考:http: //www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
回答by user459118
Create file replace.vbs:
创建文件replace.vbs:
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText 'WriteLine adds extra CR/LF
objFile.Close
To use this revised script (which we'll call replace.vbs) just type a command similar to this from the command prompt:
要使用这个修改后的脚本(我们将其称为 replace.vbs),只需在命令提示符下键入与此类似的命令:
cscript replace.vbs "C:\Scripts\Text.txt" "Jim " "James "
cscript replace.vbs "C:\Scripts\Text.txt" "Jim " "James "
回答by morechilli
BatchSubstitute.bat
on dostips.comis an example of search and replace using a pure batch file.
BatchSubstitute.bat
dostips.com 上是一个使用纯批处理文件进行搜索和替换的示例。
It uses a combination of FOR
, FIND
and CALL SET
.
它使用FOR
,FIND
和的组合CALL SET
。
Lines containing characters among "&<>]|^
may be treated incorrectly.
包含字符的行"&<>]|^
可能会被错误处理。
回答by dbenham
Note- Be sure to see the update at the end of this answer for a link to the superior JREPL.BAT that supersedes REPL.BAT
JREPL.BAT 7.0 and abovenatively supports unicode (UTF-16LE) via the /UTF
option, as well as any other character set, including UTF-8, via ADO!!!!
注意- 请务必查看本答案末尾的更新,以获取取代 REPL.BAT 的上级 JREPL.BAT 的链接
JREPL.BAT 7.0 及更高版本通过/UTF
选项本机支持 unicode (UTF-16LE) ,以及任何其他字符集,包括 UTF-8,通过 ADO!!!!
I have written a small hybrid JScript/batch utility called REPL.BATthat is very convenient for modifying ASCII (or extended ASCII) files via the command line or a batch file. The purely native script does not require installation of any 3rd party executeable, and it works on any modern Windows version from XP onward. It is also very fast, especially when compared to pure batch solutions.
我编写了一个名为 REPL.BAT 的小型混合 JScript/批处理实用程序,它非常方便通过命令行或批处理文件修改 ASCII(或扩展 ASCII)文件。纯原生脚本不需要安装任何 3rd 方可执行文件,它适用于 XP 以后的任何现代 Windows 版本。它也非常快,尤其是与纯批量解决方案相比时。
REPL.BAT simply reads stdin, performs a JScript regex search and replace, and writes the result to stdout.
REPL.BAT 只是读取标准输入,执行 JScript 正则表达式搜索和替换,并将结果写入标准输出。
Here is a trivial example of how to replace foo with bar in test.txt, assuming REPL.BAT is in your current folder, or better yet, somewhere within your PATH:
这是一个关于如何在 test.txt 中用 bar 替换 foo 的简单示例,假设 REPL.BAT 在您的当前文件夹中,或者更好的是,在您的 PATH 中的某个位置:
type test.txt|repl "foo" "bar" >test.txt.new
move /y test.txt.new test.txt
The JScript regex capabilities make it very powerful, especially the ability of the replacement text to reference captured substrings from the search text.
JScript 正则表达式功能使其非常强大,尤其是替换文本能够从搜索文本中引用捕获的子字符串。
I've included a number of options in the utility that make it quite powerful. For example, combining the M
and X
options enable modification of binary files! The M
Multi-line option allows searches across multiple lines. The X
eXtended substitution pattern option provides escape sequences that enable inclusion of any binary value in the replacement text.
我在实用程序中包含了许多选项,使其非常强大。例如,结合M
和X
选项可以修改二进制文件!在M
多行选项允许跨多行搜索。的X
扩展取代模式选项提供了转义序列,使包含在所述替代文本的任何二进制值。
The entire utility could have been written as pure JScript, but the hybrid batch file eliminates the need to explicitly specify CSCRIPT every time you want to use the utility.
整个实用程序可以编写为纯 JScript,但混合批处理文件消除了每次要使用该实用程序时都显式指定 CSCRIPT 的需要。
Here is the REPL.BAT script. Full documentation is embedded within the script.
这是 REPL.BAT 脚本。完整的文档嵌入在脚本中。
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
::************ Documentation ***********
::REPL.BAT version 6.2
:::
:::REPL Search Replace [Options [SourceVar]]
:::REPL /?[REGEX|REPLACE]
:::REPL /V
:::
::: Performs a global regular expression search and replace operation on
::: each line of input from stdin and prints the result to stdout.
:::
::: Each parameter may be optionally enclosed by double quotes. The double
::: quotes are not considered part of the argument. The quotes are required
::: if the parameter contains a batch token delimiter like space, tab, comma,
::: semicolon. The quotes should also be used if the argument contains a
::: batch special character like &, |, etc. so that the special character
::: does not need to be escaped with ^.
:::
::: If called with a single argument of /?, then prints help documentation
::: to stdout. If a single argument of /?REGEX, then opens up Microsoft's
::: JScript regular expression documentation within your browser. If a single
::: argument of /?REPLACE, then opens up Microsoft's JScript REPLACE
::: documentation within your browser.
:::
::: If called with a single argument of /V, case insensitive, then prints
::: the version of REPL.BAT.
:::
::: Search - By default, this is a case sensitive JScript (ECMA) regular
::: expression expressed as a string.
:::
::: JScript regex syntax documentation is available at
::: http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx
:::
::: Replace - By default, this is the string to be used as a replacement for
::: each found search expression. Full support is provided for
::: substituion patterns available to the JScript replace method.
:::
::: For example, $& represents the portion of the source that matched
::: the entire search pattern, represents the first captured
::: submatch, the second captured submatch, etc. A $ literal
::: can be escaped as $$.
:::
::: An empty replacement string must be represented as "".
:::
::: Replace substitution pattern syntax is fully documented at
::: http://msdn.microsoft.com/en-US/library/efy6s3e6(v=vs.80).aspx
:::
::: Options - An optional string of characters used to alter the behavior
::: of REPL. The option characters are case insensitive, and may
::: appear in any order.
:::
::: A - Only print altered lines. Unaltered lines are discarded.
::: If the S options is present, then prints the result only if
::: there was a change anywhere in the string. The A option is
::: incompatible with the M option unless the S option is present.
:::
::: B - The Search must match the beginning of a line.
::: Mostly used with literal searches.
:::
::: E - The Search must match the end of a line.
::: Mostly used with literal searches.
:::
::: I - Makes the search case-insensitive.
:::
::: J - The Replace argument represents a JScript expression.
::: The expression may access an array like arguments object
::: named $. However, $ is not a true array object.
:::
::: The $.length property contains the total number of arguments
::: available. The $.length value is equal to n+3, where n is the
::: number of capturing left parentheses within the Search string.
:::
::: $[0] is the substring that matched the Search,
::: $[1] through $[n] are the captured submatch strings,
::: $[n+1] is the offset where the match occurred, and
::: $[n+2] is the original source string.
:::
::: Arguments $[0] through $[10] may be abbreviated as
::: through . Argument $[11] and above must use the square
::: bracket notation.
:::
::: L - The Search is treated as a string literal instead of a
::: regular expression. Also, all $ found in the Replace string
::: are treated as $ literals.
:::
::: M - Multi-line mode. The entire contents of stdin is read and
::: processed in one pass instead of line by line, thus enabling
::: search for \n. This also enables preservation of the original
::: line terminators. If the M option is not present, then every
::: printed line is terminated with carriage return and line feed.
::: The M option is incompatible with the A option unless the S
::: option is also present.
:::
::: Note: If working with binary data containing NULL bytes,
::: then the M option must be used.
:::
::: S - The source is read from an environment variable instead of
::: from stdin. The name of the source environment variable is
::: specified in the next argument after the option string. Without
::: the M option, ^ anchors the beginning of the string, and $ the
::: end of the string. With the M option, ^ anchors the beginning
::: of a line, and $ the end of a line.
:::
::: V - Search and Replace represent the name of environment
::: variables that contain the respective values. An undefined
::: variable is treated as an empty string.
:::
::: X - Enables extended substitution pattern syntax with support
::: for the following escape sequences within the Replace string:
:::
::: \ - Backslash
::: \b - Backspace
::: \f - Formfeed
::: \n - Newline
::: \q - Quote
::: \r - Carriage Return
::: \t - Horizontal Tab
::: \v - Vertical Tab
::: \xnn - Extended ASCII byte code expressed as 2 hex digits
::: \unnnn - Unicode character expressed as 4 hex digits
:::
::: Also enables the \q escape sequence for the Search string.
::: The other escape sequences are already standard for a regular
::: expression Search string.
:::
::: Also modifies the behavior of \xnn in the Search string to work
::: properly with extended ASCII byte codes.
:::
::: Extended escape sequences are supported even when the L option
::: is used. Both Search and Replace support all of the extended
::: escape sequences if both the X and L opions are combined.
:::
::: Return Codes: 0 = At least one change was made
::: or the /? or /V option was used
:::
::: 1 = No change was made
:::
::: 2 = Invalid call syntax or incompatible options
:::
::: 3 = JScript runtime error, typically due to invalid regex
:::
::: REPL.BAT was written by Dave Benham, with assistance from DosTips user Aacini
::: to get \xnn to work properly with extended ASCII byte codes. Also assistance
::: from DosTips user penpen diagnosing issues reading NULL bytes, along with a
::: workaround. REPL.BAT was originally posted at:
::: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
:::
::************ Batch portion ***********
@echo off
if .%2 equ . (
if "%~1" equ "/?" (
<"%~f0" cscript //E:JScript //nologo "%~f0" "^:::" "" a
exit /b 0
) else if /i "%~1" equ "/?regex" (
explorer "http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx"
exit /b 0
) else if /i "%~1" equ "/?replace" (
explorer "http://msdn.microsoft.com/en-US/library/efy6s3e6(v=vs.80).aspx"
exit /b 0
) else if /i "%~1" equ "/V" (
<"%~f0" cscript //E:JScript //nologo "%~f0" "^::(REPL\.BAT version)" "" a
exit /b 0
) else (
call :err "Insufficient arguments"
exit /b 2
)
)
echo(%~3|findstr /i "[^SMILEBVXAJ]" >nul && (
call :err "Invalid option(s)"
exit /b 2
)
echo(%~3|findstr /i "M"|findstr /i "A"|findstr /vi "S" >nul && (
call :err "Incompatible options"
exit /b 2
)
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
:err
>&2 echo ERROR: %~1. Use REPL /? to get help.
exit /b
************* JScript portion **********/
var rtn=1;
try {
var env=WScript.CreateObject("WScript.Shell").Environment("Process");
var args=WScript.Arguments;
var search=args.Item(0);
var replace=args.Item(1);
var options="g";
if (args.length>2) options+=args.Item(2).toLowerCase();
var multi=(options.indexOf("m")>=0);
var alterations=(options.indexOf("a")>=0);
if (alterations) options=options.replace(/a/g,"");
var srcVar=(options.indexOf("s")>=0);
if (srcVar) options=options.replace(/s/g,"");
var jexpr=(options.indexOf("j")>=0);
if (jexpr) options=options.replace(/j/g,"");
if (options.indexOf("v")>=0) {
options=options.replace(/v/g,"");
search=env(search);
replace=env(replace);
}
if (options.indexOf("x")>=0) {
options=options.replace(/x/g,"");
if (!jexpr) {
replace=replace.replace(/\\/g,"\B");
replace=replace.replace(/\q/g,"\"");
replace=replace.replace(/\x80/g,"\u20AC");
replace=replace.replace(/\x82/g,"\u201A");
replace=replace.replace(/\x83/g,"\u0192");
replace=replace.replace(/\x84/g,"\u201E");
replace=replace.replace(/\x85/g,"\u2026");
replace=replace.replace(/\x86/g,"\u2020");
replace=replace.replace(/\x87/g,"\u2021");
replace=replace.replace(/\x88/g,"\u02C6");
replace=replace.replace(/\x89/g,"\u2030");
replace=replace.replace(/\x8[aA]/g,"\u0160");
replace=replace.replace(/\x8[bB]/g,"\u2039");
replace=replace.replace(/\x8[cC]/g,"\u0152");
replace=replace.replace(/\x8[eE]/g,"\u017D");
replace=replace.replace(/\x91/g,"\u2018");
replace=replace.replace(/\x92/g,"\u2019");
replace=replace.replace(/\x93/g,"\u201C");
replace=replace.replace(/\x94/g,"\u201D");
replace=replace.replace(/\x95/g,"\u2022");
replace=replace.replace(/\x96/g,"\u2013");
replace=replace.replace(/\x97/g,"\u2014");
replace=replace.replace(/\x98/g,"\u02DC");
replace=replace.replace(/\x99/g,"\u2122");
replace=replace.replace(/\x9[aA]/g,"\u0161");
replace=replace.replace(/\x9[bB]/g,"\u203A");
replace=replace.replace(/\x9[cC]/g,"\u0153");
replace=replace.replace(/\x9[dD]/g,"\u009D");
replace=replace.replace(/\x9[eE]/g,"\u017E");
replace=replace.replace(/\x9[fF]/g,"\u0178");
replace=replace.replace(/\b/g,"\b");
replace=replace.replace(/\f/g,"\f");
replace=replace.replace(/\n/g,"\n");
replace=replace.replace(/\r/g,"\r");
replace=replace.replace(/\t/g,"\t");
replace=replace.replace(/\v/g,"\v");
replace=replace.replace(/\x[0-9a-fA-F]{2}|\u[0-9a-fA-F]{4}/g,
function(jrepl "foo" "bar" /f test.txt /o -
,,){
return String.fromCharCode(parseInt("0x"+sed -c s/FOO/BAR/g filename
.substring(2)));
}
);
replace=replace.replace(/\B/g,"\");
}
search=search.replace(/\\/g,"\B");
search=search.replace(/\q/g,"\"");
search=search.replace(/\x80/g,"\u20AC");
search=search.replace(/\x82/g,"\u201A");
search=search.replace(/\x83/g,"\u0192");
search=search.replace(/\x84/g,"\u201E");
search=search.replace(/\x85/g,"\u2026");
search=search.replace(/\x86/g,"\u2020");
search=search.replace(/\x87/g,"\u2021");
search=search.replace(/\x88/g,"\u02C6");
search=search.replace(/\x89/g,"\u2030");
search=search.replace(/\x8[aA]/g,"\u0160");
search=search.replace(/\x8[bB]/g,"\u2039");
search=search.replace(/\x8[cC]/g,"\u0152");
search=search.replace(/\x8[eE]/g,"\u017D");
search=search.replace(/\x91/g,"\u2018");
search=search.replace(/\x92/g,"\u2019");
search=search.replace(/\x93/g,"\u201C");
search=search.replace(/\x94/g,"\u201D");
search=search.replace(/\x95/g,"\u2022");
search=search.replace(/\x96/g,"\u2013");
search=search.replace(/\x97/g,"\u2014");
search=search.replace(/\x98/g,"\u02DC");
search=search.replace(/\x99/g,"\u2122");
search=search.replace(/\x9[aA]/g,"\u0161");
search=search.replace(/\x9[bB]/g,"\u203A");
search=search.replace(/\x9[cC]/g,"\u0153");
search=search.replace(/\x9[dD]/g,"\u009D");
search=search.replace(/\x9[eE]/g,"\u017E");
search=search.replace(/\x9[fF]/g,"\u0178");
if (options.indexOf("l")>=0) {
search=search.replace(/\b/g,"\b");
search=search.replace(/\f/g,"\f");
search=search.replace(/\n/g,"\n");
search=search.replace(/\r/g,"\r");
search=search.replace(/\t/g,"\t");
search=search.replace(/\v/g,"\v");
search=search.replace(/\x[0-9a-fA-F]{2}|\u[0-9a-fA-F]{4}/g,
function(DEL New.txt
setLocal EnableDelayedExpansion
For /f "tokens=* delims= " %%a in (OLD.txt) do (
Set str=%%a
set str=!str:FOO=BAR!
echo !str!>>New.txt
)
ENDLOCAL
,,){
return String.fromCharCode(parseInt("0x"+REM DE-DUPLICATE THE Mapping.txt FILE
REM THE DE-DUPLICATED FILE IS STORED AS new.txt
set MapFile=Mapping.txt
set ReplaceFile=New.txt
del %ReplaceFile%
::DelDupeText.bat
rem https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/sj8IUhMOq6o
setLocal EnableDelayedExpansion
for /f "tokens=1,2 delims=," %%a in (%MapFile%) do (
set str=%%a
rem Ref: http://www.dostips.com/DtTipsStringManipulation.php#Snippets.RightString
set str=!str:~-9!
set str2=%%a
set str3=%%a,%%b
find /i ^"!str!^" %MapFile%
find /i ^"!str!^" %ReplaceFile%
if errorlevel 1 echo !str3!>>%ReplaceFile%
)
ENDLOCAL
.substring(2)));
}
);
search=search.replace(/\B/g,"\");
} else search=search.replace(/\B/g,"\\");
}
if (options.indexOf("l")>=0) {
options=options.replace(/l/g,"");
search=search.replace(/([.^$*+?()[{\|])/g,"\");
if (!jexpr) replace=replace.replace(/$/g,"$$$$");
}
if (options.indexOf("b")>=0) {
options=options.replace(/b/g,"");
search="^"+search
}
if (options.indexOf("e")>=0) {
options=options.replace(/e/g,"");
search=search+"$"
}
var search=new RegExp(search,options);
var str1, str2;
if (srcVar) {
str1=env(args.Item(3));
str2=str1.replace(search,jexpr?replFunc:replace);
if (!alterations || str1!=str2) if (multi) {
WScript.Stdout.Write(str2);
} else {
WScript.Stdout.WriteLine(str2);
}
if (str1!=str2) rtn=0;
} else if (multi){
var buf=1024;
str1="";
while (!WScript.StdIn.AtEndOfStream) {
str1+=WScript.StdIn.Read(buf);
buf*=2
}
str2=str1.replace(search,jexpr?replFunc:replace);
WScript.Stdout.Write(str2);
if (str1!=str2) rtn=0;
} else {
while (!WScript.StdIn.AtEndOfStream) {
str1=WScript.StdIn.ReadLine();
str2=str1.replace(search,jexpr?replFunc:replace);
if (!alterations || str1!=str2) WScript.Stdout.WriteLine(str2);
if (str1!=str2) rtn=0;
}
}
} catch(e) {
WScript.Stderr.WriteLine("JScript runtime error: "+e.message);
rtn=3;
}
WScript.Quit(rtn);
function replFunc(##代码##, , , , , , , , , , ) {
var $=arguments;
return(eval(replace));
}
IMPORTANT UPDATE
重要更新
I have ceased development of REPL.BAT, and replaced it with JREPL.BAT. This newer utility has all the same functionality of REPL.BAT, plus much more:
我已经停止了 REPL.BAT 的开发,并用 JREPL.BAT 取而代之。这个较新的实用程序具有 REPL.BAT 的所有相同功能,以及更多功能:
- Unicode UTF-16LE support via native CSCRIPT unicode capabilities, and any other character set (including UTF-8) via ADO.
- Read directly from / write directly to a file: no need for pipes, redirection, or move command.
- Incorporate user supplied JScript
- Translation facility similar to unix tr, only it also supports regex search and JScript replace
- Discard non-matching text
- Prefix output lines with line number
- and more...
- 通过本机 CSCRIPT unicode 功能支持 Unicode UTF-16LE,通过 ADO 支持任何其他字符集(包括 UTF-8)。
- 直接读取/直接写入文件:不需要管道、重定向或移动命令。
- 合并用户提供的 JScript
- 类似于 unix tr 的翻译工具,只是它还支持正则表达式搜索和 JScript 替换
- 丢弃不匹配的文本
- 用行号前缀输出行
- 和更多...
As always, full documentation is embedded within the script.
与往常一样,完整的文档嵌入在脚本中。
The original trivial solution is now even simpler:
原来的微不足道的解决方案现在更简单:
##代码##The current version of JREPL.BAT is available at DosTips. Read all of the subsequent posts in the thread to see examples of usage and a history of the development.
当前版本的 JREPL.BAT 可从 DosTips 获得。阅读线程中的所有后续帖子,以查看使用示例和开发历史。
回答by Aman
Use FNR
使用 FNR
Use the fnr
utility. It's got some advantages over fart
:
使用该fnr
实用程序。它有一些优点fart
:
- Regular expressions
- Optional GUI. Has a "Generate command line button" to create command line text to put in batch file.
- Multi-line patterns: The GUI allows you to easily work with multi-line patterns. In FART you'd have to manually escape line breaks.
- Allows you to select text file encoding. Also has an auto detect option.
- 常用表达
- 可选的图形用户界面。有一个“生成命令行按钮”来创建命令行文本以放入批处理文件。
- 多线模式:GUI 允许您轻松使用多线模式。在 FART 中,您必须手动转义换行符。
- 允许您选择文本文件编码。还有一个自动检测选项。
Download FNR here: http://findandreplace.io/?z=codeplex
在此处下载 FNR:http: //findandreplace.io/?z= codeplex
Usage example:
fnr --cl --dir "<Directory Path>" --fileMask "hibernate.*" --useRegEx --find "find_str_expression" --replace "replace_string"
用法示例:
fnr --cl --dir "<Directory Path>" --fileMask "hibernate.*" --useRegEx --find "find_str_expression" --replace "replace_string"
回答by Ferruccio
回答by Leptonator
I know I am late to the party..
我知道我参加聚会迟到了..
Personally, I like the solution at: - http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
就个人而言,我喜欢以下解决方案: - http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
We also, use the Dedupe Function extensively to help us deliver approximately 500 e-mails daily via SMTP from: - https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/sj8IUhMOq6o
我们还广泛使用重复数据删除功能来帮助我们每天通过 SMTP 发送大约 500 封电子邮件: - https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/sj8IUhMOq6o
and these both work natively with no extra tools or utilities needed.
这些都可以在本地工作,不需要额外的工具或实用程序。
REPLACER:
替代品:
##代码##DEDUPLICATOR (note the use of -9 for an ABA number):
DEDUPLICATOR(注意使用 -9 表示 ABA 数字):
##代码##Thanks!
谢谢!