views:

406

answers:

9

Is there a simple and lightweight program to search over a text file and replace a string with regex?

+6  A: 

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.

Epaga
Modern Macs are based on Unix and have `sed` and `awk` installed as standard.
pdc
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
+1  A: 

Many decent text editors have the option as well, vim, emacs, EditPlus and so on.

Vinko Vrsalovic
+1  A: 

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...

Federico Ramponi
+2  A: 

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
Paul Dixon
A: 

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 similaor constructions. I'm not sure of any modern editors that support this fucntionality.

Tip: Try out a simple replacement first, it can be a bit unclear as you might up add '\' to escape the special RegExp constructions...

Roalt
Huh? Is emacs "simple and lightweight"? Argh! :^)
Federico Ramponi
+3  A: 

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.

Andy Lester
great field guide. somehow over the last 7 years i have never known about -l or -p...
matpalm
A: 

You didn't mention what platform you're using... If you are interested in a relatively simple GUI tool, there's regexxer. Otherwise, the commandline tools such as sed that were mentioned earlier can be very useful.

jonner
A: 

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.

warren
A: 

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.

Odilon Redo