Is there a simple and lightweight program to search over a text file and replace a string with regex?
10 Answers
For searching: grep - simple and fast. Included with Linux, here's a Windows version, not sure about Mac.
For replacing: sed. Here's a Windows version, not sure about Mac.
Of course, if you want to actually open up a file and see its contents while you search and replace, you can use emacs for that. Or ConTEXT. Or vim. Or what have you. ;)
See also this question.
-
Modern Macs are based on Unix and have `sed` and `awk` installed as standard. – pdc Oct 27 '08 at 10:19
-
If a sed version that supports a binary mode is needed, the Windows versions won't cut it. However, the cygwin version of sed 4.1.5 supports a binary mode. – Shadow2531 Oct 27 '08 at 11:22
Perl excels at this, with its -i, -n, -p and -e switches. See the slides from my talk Field Guide To The Perl Command Line Switches for examples.
Others have mentioned sed and awk, and it's no surprise that Perl was inspired by them. However, Perl may well be easier to get and install for you and/or your users.

- 91,102
- 13
- 100
- 152
-
great field guide. somehow over the last 7 years i have never known about -l or -p... – mat kelcey Oct 09 '08 at 07:40
There's also sed, which is a useful tool to learn the basics of - great for doing quick regex based substitutions.
Quick example, to change "foo" to "bar" in input.txt ...
sed -e 's/foo/bar/g' input.txt > output.txt

- 295,876
- 54
- 310
- 348
Many decent text editors have the option as well, vim, emacs, EditPlus and so on.

- 330,807
- 53
- 334
- 373
sed or awk. I recommend the book sed&awk to master the subject or the booklet sed&awk pocket reference for a quick reference. Of course mastering regular expressions is a must...

- 46,145
- 29
- 109
- 133
It depends if you're dealing with one or many files. At the risk of being pilloried, I'm assuming you're using Windows because you didn't specify a platform.
For one file at a time, Notepad2 does the trick and is extremely fast, lightweight and portable.
For search/replace over multiple files at once, try Agent Ransack.

- 551
- 4
- 5
-
Agent Ransack doesn't support replacing functionality directly, but you could open all found files in Notepad++ and make a Replace in all open Documents! – leon22 Jul 23 '19 at 08:58
Try WildGem: http://www.skytopia.com/software/wildgem
I'm the creator. Small, super-fast, portable and self-contained. You can use Regex, but it also has its own simple language syntax to make queries much easier in theory.
I quote:
Unlike similar programs, WildGem is fast with a dual split display, and updates or highlights matches as you type in realtime. A unique colour coded syntax allows you to easily find/replace text without worrying about having to escape special symbols.

- 3,520
- 7
- 42
- 69
Use emacs or xemacs. It has a perfect regexp replacement function. You can even use constructions like /1 (or /2 or /3) to get a matched expression back in your replacement that was identified with ( ) around them. To prevent a vi-emacs clash: vi will also have similar constructions. I'm not sure of any modern editors that support this functionality.
Tip: Try out a simple replacement first, it can be a bit unclear as you might up add '\' to escape the special RegExp constructions...
NOt knowing the platform, I'd say the ad that popped-up pon this page might be appropriate: PowerGREP. Don't know anything about it, but it sounds similar to what you're looking for.

- 32,620
- 21
- 85
- 124