- Windows: Find and Replace searches multiple files on your system for a string of text and replaces it with whatever you choose. Think of it like the Find/Replace tool in your favorite text editor.
- Finding and Replacing Text in Code Editor. To find or replace text in the Code Editor, use the appropriate commands of the TestComplete Edit menu: Find (default shortcut is Ctrl+F). This menu item opens the Find dialog, where you can specify the following search options.
- Windows Find And Replace Text In Files
- Windows Find And Replace Text In Excel
- Find And Replace Text Tool
- Windows Cmd Find And Replace Text
- Windows Batch Find And Replace Text
Jun 26, 2019 Need to know how to use PowerShell to replace text in a file? Look no further! This blog post is for you. By the end of the post, I'll show you a function I built to make your life much easier. Replacing text in a file with PowerShell is three-step process. Reading the file Finding and replacing the.
To help you find text, Word offers a handy Find feature. Not only can this Find feature search for a word or phrase, but it also offers a Replace option so you can make Word find certain words and automatically replace them with other words.
Using the Find command in Word 2019
The Find command can search for a single character, a word, or a group of words. To make searching faster, you can search either an entire document or just a specific part of a document. To make searching a document more flexible, Word lets you search also for headings or pages.
To search for words or phrases by using Word’s Find command, follow these steps:
- Click the Home tab.
- Click the Find icon in the Editing group.
The Navigation pane appears on the left side of the screen.If you click the downward-pointing arrow to the right of the Find icon, a menu appears that lets you choose the Find or Go To command. - Click in the Navigation text box, type a word or phrase to find, and press Enter.The Navigation pane lists all matching text.As you type, Word displays all matching text. So if you start typing hel, Word will find all text that matches hel, such as hello, helicopter, or help.
- Click any of the text displayed in the Navigation pane.Word highlights your chosen text in your document.
- To close the Navigation pane, click the X icon in the upper-right corner of the pane.
How to customize text searching in Word 2019
If you just want to find a word or phrase, the ordinary Find command works. However, if Word finds too much irrelevant text, you may want to take time to customize how Word searches for text. Follow these steps:
- Click the Home tab.
- Click the Find icon in the Editing group.The Navigation pane appears on the left side of the screen.
- In the Navigation pane’s Search Document text box, click the magnifying glass icon.
A pull-down menu appears. - Click Options.A Find Options dialog box appears.
- Select one or more options in the Find Options dialog box:
- Match Case: Finds text that exactly matches the uppercase and lowercase letters you type.
- Find Whole Words Only: Finds text that is not part of another word. Searching for on will not find words such as onion.
- Use Wildcards: Lets you use the single character (?) and multiple character (*) wildcards, such as searching for d?g, which will find dog or dig; or b*t, which will find but, butt, or boost.
- Sounds Like: Searches for words based on their phonetic pronunciation, such as finding elephant when searching for elefant.
- Find All Word Forms: Finds all variations of a word, such as finding run, ran, and running.
- Highlight All: Highlights all matches in the document. This option works with Incremental find.
- Incremental Find: Constantly highlights words as you type. If you turn off this option, Word will highlight words in a document only after you finish typing and press Enter.
- Match Prefix: Searches for the prefix of words, such as finding interact by searching for inter.
- Match Suffix: Searches for the suffix of words, such as finding runner or keeper just by searching for er.
- Ignore Punctuation Characters: Ignores punctuation characters in text, such as finding the phrase Hello, there when you searched for Hello there.
- Ignore white-space characters: Ignores spaces when searching, such as finding the phrase BotheCat when you searched for Bo the Cat.
- Click OK to make the Find Options dialog box disappear.
The next time you search for text, Word will use the last options you chose.
How to search by headings in Word 2019
Rather than search for a word or phrase, you may want to browse a long document by headings. After you find the heading you want, you can edit or read the text below that heading.
To search by headings in Word, follow these steps:
- Click the Home tab.
- Click the Find icon in the Editing group.
The Navigation pane appears on the left side of the screen. - In the Navigation pane, click the Headings tab (below the Search Document text box).
A list of headings appears in the Navigation pane. - Click a heading in the Navigation pane.
Word displays the heading in your document.
Browsing through pages in Word 2019
In a long document, you often have to scroll or flip through multiple pages to find specific text. To simplify this task, Word can display all pages as thumbnail images. You can browse through these thumbnail images and click the page that you want to view in more detail.
To browse through multiple pages in Word, follow these steps:
- Click the Home tab.
- Click the Find icon in the Editing group.The Navigation pane appears on the left side of the screen.
- In the Navigation pane, click the Pages tab (the middle tab).
Word displays thumbnail images of all your pages. - Click the thumbnail image of the page that you want to view.Word displays your chosen page.
Using the Find and Replace command in Word 2019
Rather than just find a word or phrase, you may want to find text and replace it with something else. To use the Find and Replace command in Word, follow these steps:
- Click the Home tab.
- Click the Replace icon in the Editing group (or press Ctrl+H).The Find and Replace dialog box appears.
- Click in the Find What text box and type a word or phrase to find.
- Click in the Replace With text box and type a word or phrase to replace the text you typed in Step3.
- (Optional) Click the More button and choose any additional options.
- Click one of the following buttons:
- Replace: Replaces the currently highlighted text.
- Replace All: Searches and replaces text throughout the entire document.
- Find Next: Searches from the current cursor location to the end of the document.
- Click Find Next to search for additional occurrences of the text you typed in Step 3.
- Click Cancel to make the Find and Replace dialog box disappear.
Active3 months ago
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?
Ray Vega
Ray VegaRay Vega86.4k9292 gold badges203203 silver badges195195 bronze badges
27 Answers
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:
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.exe should be part of your PATH statement already, but if not you can add it. The location of it on my machine is
RachelRachelC:WINDOWSsystem32WindowsPowerShellv1.0
101k5252 gold badges259259 silver badges428428 bronze badges
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.
Michael Haren79.1k3838 gold badges152152 silver badges198198 bronze badges
Mike SchallMike Schall4,87622 gold badges3333 silver badges4646 bronze badges
Just used FART ('F ind A nd R eplace T ext' command line utility):
excellent little freeware for text replacement within a large set of files.
excellent little freeware for text replacement within a large set of files.
The setup files are on SourceForge.
Usage example:
will preview the replacements to do recursively in the files of this Perl distribution.
Only problem: the FART website icon isn't exactly tasteful, refined or elegant ;)
Update 2017 (7 years later) jagb points out in the comments to the 2011 article 'FARTing the Easy Way – Find And Replace Text' from Mikail Tunç
Community♦
VonCVonC896k330330 gold badges29092909 silver badges34993499 bronze badges
Replace - Replace a substring using string substitutionDescription: 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
RB.29.7k99 gold badges7272 silver badges107107 bronze badges
Bill RichardsonBill Richardson1,28311 gold badge88 silver badges22 bronze badges
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:ScriptsText.txt' 'Jim ' 'James '
3,22711 gold badge2626 silver badges3232 bronze badges
user459118user459118
BatchSubstitute.bat
on dostips.com is an example of search and replace using a pure batch file.It uses a combination of
FOR
, FIND
and CALL SET
.Lines containing characters among
Gilles'&<>]|^
may be treated incorrectly.80.3k2020 gold badges171171 silver badges210210 bronze badges
morechillimorechilli7,19066 gold badges2929 silver badges5151 bronze badges
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
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
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.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:
- 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...
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.
dbenhamdbenham107k2020 gold badges191191 silver badges295295 bronze badges
Use the
fnr
utility. It's got some advantages over 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.
Download FNR here: http://findandreplace.io/?z=codeplex
Usage example:
fnr --cl --dir '<Directory Path>' --fileMask 'hibernate.*' --useRegEx --find 'find_str_expression' --replace 'replace_string'
AmanAman
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
Patsed
command (or download only sed
):10.6k1111 gold badges7474 silver badges100100 bronze badges
FerruccioFerruccio83k3535 gold badges205205 silver badges287287 bronze badges
I know I am late to the party..
Personally, I like the solution at: - 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
and these both work natively with no extra tools or utilities needed.
REPLACER:
DEDUPLICATOR (note the use of -9 for an ABA number):
Thanks!
LeptonatorLeptonator
When you work with Git on Windows then simply fire up
git-bash
and use sed
. Or, when using Windows 10, start 'Bash on Ubuntu on Windows' (from the Linux subsystem) and use sed
.Its a stream editor, but can edit files directly by using the following command:
-i
option is used to edit in place on filename.-e
option indicates a command to run.s
is used to replace the found expression 'foo' with 'bar' andg
is used to replace any found matches.
Note by ereOn:
If you want to replace a string in versioned files only of a Git repository, you may want to use:
git ls-files <eventual subfolders & filters> | xargs sed -i -e 's/foo/bar/g'
which works wonders.
Jens A. KochJens A. Koch28.5k88 gold badges9191 silver badges104104 bronze badges
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
CoolBeans18.8k1010 gold badges7575 silver badges9696 bronze badges
FaisalFaisal
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... :-(
Simon EastSimon East37k1111 gold badges110110 silver badges106106 bronze badges
With the replacer.bat
1) With
e?
option that will evaluate special character sequences like nr
and unicode sequences. In this case will replace quoted 'Foo'
and 'Bar'
:2) Straightforward replacing where the
npocmakanpocmakaFoo
and Bar
are not quoted.43.8k1111 gold badges9696 silver badges137137 bronze badges
Here's a solution that I found worked on Win XP. In my running batch file, I included the following:
The
replace.bat
file is as below. I did not find a way to include that function within the same batch file, because the %%a
variable always seems to give the last value in the for loop.replace.bat
:80.3k2020 gold badges171171 silver badges210210 bronze badges
ChadChad
Take a look at Is there any sed like utility for cmd.exe which asked for a sed equivalent under Windows, should apply to this question as well. Executive summary:
- It can be done in batch file, but it's not pretty
- Lots of available third party executables that will do it for you, if you have the luxury of installing or just copying over an exe
- Can be done with VBScript or similar if you need something able to run on a Windows box without modification etc.
Community♦
JayJay36.2k1111 gold badges5656 silver badges8181 bronze badges
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.
Peter SchuetzePeter Schuetze14.6k33 gold badges3333 silver badges5656 bronze badges
Two batch files that supply
search and replace
functions have been written by Stack Overflow members dbenham
and aacini
using native built-in jscript
in Windows. They are both
robust
and very swift with large files
compared to plain batch scripting, and also simpler
to use for basic replacing of text. They both have Windows regular expression
pattern matching.Windows Find And Replace Text In Files
- This
sed-like
helper batch file is calledrepl.bat
(by dbenham).Example using theL
literal switch: - This
grep-like
helper batch file is calledfindrepl.bat
(by aacini).Example which has regular expressions active:
Both become powerful system-wide utilities
when placed in a folder that is on the path
, or can be used in the same folder with a batch file, or from the cmd prompt.Windows Find And Replace Text In Excel
They both have
Usman Maqboolcase-insensitive
switches and also many other functions.2,41277 gold badges2323 silver badges3535 bronze badges
foxidrivefoxidrive34.9k77 gold badges3838 silver badges6464 bronze badges
Cody Gray♦201k3838 gold badges405405 silver badges488488 bronze badges
kool_guy_herekool_guy_here
Just faced a similar problem - 'Search and replace text within files', but with the exception that for both filenames and search/repalce I need to use regex. Because I'm not familiar with Powershell and want to save my searches for later use I need something more 'user friendly' (preferable if it has GUI).
So, while Googling :) I found a great tool - FAR (Find And Replace) (not FART).
That little program has nice GUI and support regex for searching in filenames and within files. Only disadventage is that if you want to save your settings you have to run the program as an administrator (at least on Win7).
jeb
61.2k1313 gold badges139139 silver badges181181 bronze badges
madcorpmadcorp
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.
Find And Replace Text Tool
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
Nick Number
@Rachel gave an excellent answer but here is a variation of it to read content to a powershell
WhomeWhome$data
variable. You may then easily manipulate content multiple times before writing to a output file. Also see how multi-line values are given in a .bat batch files.8,18033 gold badges3131 silver badges5757 bronze badges
Use powershell in .bat - for Windows 7+
encoding utf8 is optional, good for web sites
Windows Cmd Find And Replace Text
Wagner PereiraWagner Pereira
Download Cygwin (free) and use unix-like commands at the Windows command line.
Your best bet: sed
jm.jm.14.7k2020 gold badges6767 silver badges8989 bronze badges
I'm prefer to use
sed
from GNU utilities for Win32, the followings need to be noted- single quote
'
won't work in windows, use'
instead sed -i
won't work in windows, it will need file swapping
So the working code of
ChetabahanaChetabahanased
to find and replace text in a file in windows is as below4,67511 gold badge2828 silver badges4949 bronze badges
Can also see the Replace and ReplaceFilter tools at https://zoomicon.github.io/tranXform/ (source included). The 2nd one is a filter.
The tool that replaces strings in files is in VBScript (needs Windows Script Host [WSH] to run in old Windows versions)
The filter is probably not working with Unicode unless you recompile with latest Delphi (or with FreePascal/Lazarus)
George BirbilisGeorge Birbilis1,80911 gold badge1515 silver badges2626 bronze badges
I have faced this problem several times while coding under Visual C++.If you have it, you can use Visual studio Find and Replace Utility. It allows you to select a folder and replace the contents of any file in that folder with any other text you want.
Under Visual Studio: Edit -> Find and Replace In the opened dialog, select your folder and fill in 'Find What' and 'Replace With' boxes.Hope this will be helpful.
NadjibNadjib
protected by Samuel Liew♦Oct 5 '15 at 9:21
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?