Umber Ferrule
Umber Ferrule

Reputation: 3366

What's the best tool to find and replace regular expressions over multiple files?

Preferably free tools if possible.

Also, the option of searching for multiple regular expressions and each replacing with different strings would be a bonus.

Upvotes: 35

Views: 27358

Answers (21)

Oli
Oli

Reputation: 239810

Under Windows, I used to like WinGrep. This no longer appears to exist, but a new project called grepwin does. I have no experience with this, so cannot recommend it.

Under Ubuntu, I used to use Regexxer but found that GUIs have to work too hard to work around the realities of regular expressions.

These days I almost always use ack.

Upvotes: 4

CodePrinz
CodePrinz

Reputation: 497

If you are a Programmer: A lot of IDEs should do a good Job as well.

For me PyCharm worked quite nice:

  • Edit > Find > Replace in Path or Strg + Shift + R
  • Check Regex at the top

It has a live preview.

Upvotes: 1

Frosty
Frosty

Reputation: 6473

sed is quick and easy:

sed -e "s/pattern/result/" <file list>

You can also join it with find:

find <other find args> -exec sed -e "s/pattern/result/" "{}" ";"

Upvotes: 27

Alex Fort
Alex Fort

Reputation: 18821

Perl.

Seriously, it makes sysadmin stuff so much easier. Here's an example:

perl -pi -e 's/something/somethingelse/g' *.log

Upvotes: 49

user742070
user742070

Reputation: 101

For at least 25 years, I've been using Emacs for large-scale replacements across large numbers of files. Run etags to specify any set of files to search through:

$ etags file1.txt file2.md dir1/*.yml dir2/*.json dir3/*.md

Then open Emacs and run tags-query-replace, which prompts for regex and replacement:

\b\(foo\)\b
\1bar

Upvotes: 0

ThorSummoner
ThorSummoner

Reputation: 18109

if 'textpad' is a valid answer, I would suggest Sublime Text hands down.

Multi-cursor edits are an even more efficient way to make replacements in general I find, but its "Find in Files" is top tier for bulk regex/plain find replacements.

Upvotes: 1

victe
victe

Reputation: 534

Brackets (source code, deb/Ubuntu, OSx and Windows) has a good visualization of results, permitting select them individually to apply substitution. You can search by standard text, case sensitive or not, and regex. Very important: you can exclude patterns of files and directories in the search.

Upvotes: -1

Patrick Steil
Patrick Steil

Reputation: 525

I love this tool:

http://www.abareplace.com/

Gives you an "as you type" preview of your regular expression... FANTASTIC for those not well versed in RE's... and it is super fast at changing hundreds or thousands of files at a time...

And then let's you UNDO your changes as well...

Very nice...

Patrick Steil - http://www.podiotools.com

Upvotes: 2

user2005187
user2005187

Reputation: 239

I've written a free command line tool for Windows to do this. It's called rxrepl, it supports unicode and file search. Some may find it useful.

Upvotes: 8

Ciantic
Ciantic

Reputation: 5764

In Windows there is free alternative that works the best: Notepad++

Go to "Search" -> "Find in Files". One may give directory, file pattern, set regular expressions then preview the matches and finally replace all files recursively.

Upvotes: 2

jhw
jhw

Reputation: 35

I have the luxury of Unix and Ubuntu; In both, I use gawk for anything that requires line-by-line search and replace, especially for line-by-line for substring(s). Recently, this was the fastest for processing 1100 changes against millions of lines in hundreds of files (one directory) On Ubuntu I am a fan of regexxer

 sudo apt-get install regexxer

Upvotes: 1

Templar
Templar

Reputation: 5135

My personal favorite is PowerGrep by JGSoft. It interfaces with RegexBuddy which can help you to create and test the regular expression, automatically backs up all changes (and provides undo capabilities), provides the ability to parse multiple directories (with filename patterns), and even supports file formats such as Microsoft Word, Excel, and PDF.

PowerGrep Screenshot

Upvotes: 2

Martin R-L
Martin R-L

Reputation: 4047

I've found the tool RxFind useful (free OSS).

Upvotes: -1

Huppie
Huppie

Reputation: 11432

For find-and-replace on multiple files on Windows I found rxFind to be very helpful.

Upvotes: 3

Zsolt Botykai
Zsolt Botykai

Reputation: 51603

Vim for the rescue (and president ;-) ). Try:

vim -c "argdo! s:foo:bar:gci" <list_of_files>

(I do love Vim's -c switch, it's magic. Or if you had already in Vim, and opened the files, e.g.:

vim <list_of_files>

Just issue:

:bufdo! s:foo:bar:gci

Of course sed and perl is capable as well. HTH.

Upvotes: 1

lindelof
lindelof

Reputation: 35240

Emacs's directory editor has the `dired-do-query-replace-regexp' function to search for and replace a regexp over a set of marked files.

Upvotes: 6

Craig Trader
Craig Trader

Reputation: 15679

Unsurprisingly, Perl does a fine job of handling this, in conjunction with a decent shell:

for file in @filelist ; do
  perl -p -i -e "s/pattern/result/g" $file
done

This has the same effect (but is more efficient, and without the race condition) as:

for file in @filelist ; do
  cat $file | sed "s/pattern/result/" > /tmp/newfile
  mv /tmp/newfile $file
done

Upvotes: 5

Oliver Giesen
Oliver Giesen

Reputation: 9439

jEdit's regex search&replace in files is pretty decent. Slightly overkill if you only use it for that, though. It also doesn't support the multi-expression-replace you asked for.

Upvotes: 1

Simon
Simon

Reputation:

For Mac OS X, TextWrangler does the job.

Upvotes: 2

levik
levik

Reputation: 117529

Textpad does a good job of it on Windows. And it's a very good editor as well.

Upvotes: 6

Rafał Dowgird
Rafał Dowgird

Reputation: 45091

I'd go for bash + find + sed.

Upvotes: 1

Related Questions