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?
|
If you are on Windows version that supports .Net 2.0, I would replace your shell. PowerShell gives 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.
|
|||||||||||||||||||||
|
Just used FART ("F ind A nd R eplace T ext" command line utility): The setup files are here.
will preview the replacements to recursively do in the files of this Perl distribution. Only problem: the FART website icon isn't exactly tasteful, refined nor elegant ;) |
|||||||||||||||
|
It uses a combination of Lines containing characters among |
|||||||||||||
|
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 |
|||||||||||||||
|
I don't think there's a way to do it with any built-in commands. I would suggest you download something like Gnuwin32 or UnxUtils and use the
|
|||||||||
|
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:
|
||||
|
Here's a solution that I found worked on Win XP. In my running batch file, I included the following:
The
|
||||
|
I have used perl, and that works marvelously.
.orig is the extension it would append to the original file For a number of files matching such as *.html
|
||||
|
May be a little bit late, but I am frequently looking for similar stuff, since I don't want to get through the pain of getting software approved. However, you usually use the FOR statement in various forms. Someone created a useful batch file that does a search and replace. Have a look here. It is important to understand the limitations of the batch file provided. For this reason I don't copy the source code in this answer. |
|||
|
Use fnr utility its better than fart since it can search and replace based on regular expressions. Also for the UI lovers you can configure options in UI and it can generate command line string which can then be used in your script. Very easy to use even as command line stirng. Find it here http://findandreplace.codeplex.com/ Example fnr --cl --dir "" --fileMask "hibernate.*" --useRegEx --find "find_str_expression" --replace "replace_string" |
|||
|
I have written a small hybrid JScript/batch utility called REPL.BAT that is very convenient for modifying 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 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.
|
||||
|
Take a look at http://stackoverflow.com/questions/127318/ which asked for a sed equivalent under Windows, should apply to this question as well. Executive summary:
|
|||
|
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... :-( |
|||
|
This is one thing that batch scripting just does not do well. The script morechilli linked to will work for some files, but unfortunately it will choke on ones which contain characters such as pipes and ampersands. VBScript is a better built-in tool for this task. See this article for an example: http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx |
|||
|
When I was looking for a similar tool for my own usage, I found many of the tools lacking. So I wrote yet another one and made it open source. http://findandreplace.codeplex.com/ The main difference with other tools is that you can easily generate a command line using windows dialog and test it through regular UI before running find/replace in DOS or in batch file. |
|||
|
Power shell command works like a charm
|
||||
|
Download Cygwin (free) and use unix-like commands at the Windows command line. Your best bet: sed |
||||