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?
相关问题
- Inheritance impossible in Windows Runtime Componen
- how to get running process information in java?
- Is TWebBrowser dependant on IE version?
- How can I have a python script safely exit itself?
- I want to trace logs using a Macro multi parameter
相关文章
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- Warning : HTML 1300 Navigation occured?
- Bundling the Windows Mono runtime with an applicat
- Windows 8.1 How to fix this obsolete code?
- Is there a non-java, cross platform way to launch
- Compile and build with single command line Java (L
- CosmosDB emulator can't start since port is al
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.
Script Output:
ref: http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
Create file replace.vbs:
To use this revised script (which we’ll call replace.vbs) just type a command similar to this from the command prompt:
cscript replace.vbs "C:\Scripts\Text.txt" "Jim " "James "
With the replacer.bat
1) With
e?
option that will evaluate special character sequences like\n\r
and unicode sequences. In this case will replace quoted"Foo"
and"Bar"
:2) Straightforward replacing where the
Foo
andBar
are not quoted.I played around with some of the existing answers here and prefer my improved solution...
or if you want to save the output again to a file...
The benefit of this is that you can pipe in output from any program. Will look into using regular expressions with this too. Couldn't work out how to make it into a BAT file for easier use though... :-(
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 above natively supports unicode (UTF-16LE) via the
/UTF
option, as well as any other character set, including UTF-8, via ADO!!!!I have written a small hybrid JScript/batch utility called REPL.BAT that 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 simply reads stdin, performs a JScript regex search and replace, and writes the result to stdout.
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:
The JScript regex capabilities make it very powerful, especially the ability of the replacement text to reference captured substrings from the search text.
I've included a number of options in the utility that make it quite powerful. For example, combining the
M
andX
options enable modification of binary files! TheM
Multi-line option allows searches across multiple lines. TheX
eXtended substitution pattern option provides escape sequences that enable inclusion of any binary value in the replacement text.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.
Here is the REPL.BAT script. Full documentation is embedded within the script.
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:
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.
Use FNR
Use the
fnr
utility. It's got some advantages overfart
:Download FNR here: http://findandreplace.codeplex.com/
Usage example:
fnr --cl --dir "<Directory Path>" --fileMask "hibernate.*" --useRegEx --find "find_str_expression" --replace "replace_string"