34

Preferably free tools if possible.

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

Umber Ferrule
  • 3,358
  • 6
  • 35
  • 38

21 Answers21

46

Perl.

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

perl -pi -e 's/something/somethingelse/g' *.log
simhumileco
  • 31,877
  • 16
  • 137
  • 115
Alex Fort
  • 18,459
  • 5
  • 42
  • 51
27

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/" "{}" ";"
simhumileco
  • 31,877
  • 16
  • 137
  • 115
Frosty
  • 6,213
  • 3
  • 24
  • 20
  • 1
    I agree on the quick.... and it is easy only for a given value of easy. Anyways +1 ;) – Mario Ortegón Jun 12 '09 at 21:17
  • 1
    Yeah, but problems begin when you have to paste 10 lines long html code with dots, slashes, quotation marks and so on to multiple files. – alekwisnia Sep 06 '11 at 12:59
  • 4
    This doesn't appear to update the files, but simply output them. The `-i` argument appears to fix that. See also http://stackoverflow.com/questions/10445934/change-multiple-files – Thomas Ahle Sep 12 '16 at 09:33
9

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.

user2005187
  • 241
  • 3
  • 3
6

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

levik
  • 114,835
  • 27
  • 73
  • 90
  • Couldn't agree more about TextPad being an excellent editor (my personal favourite). However, whilst it does search over multiple files (and very quickly at that) it doesn't do search and replace over multiple files. – Umber Ferrule Sep 21 '08 at 09:07
  • 6
    Yes it does. What you have to do is right-click and select "Open All" in the search results. This will open all the found files. Then in the Replace dialog, click the "All Documents" radio button for where to do the replacing. After it finishes, click "Save All". – levik Sep 23 '08 at 13:52
6

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.

lindelof
  • 34,556
  • 31
  • 99
  • 140
5

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
Craig Trader
  • 15,507
  • 6
  • 37
  • 55
4

Under Windows, I used to like WinGrep

Under Ubuntu, I use Regexxer.

Oli
  • 235,628
  • 64
  • 220
  • 299
  • 1
    WinGrep is no longer available. Now you can use [grepwin](https://sourceforge.net/projects/grepwin/) instead. – DanielTuzes Mar 03 '17 at 09:53
3

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

Community
  • 1
  • 1
Huppie
  • 11,263
  • 4
  • 32
  • 34
2

For Mac OS X, TextWrangler does the job.

  • Especially because it provides visual feedback and allows you to review the changes it made before saving them back to files. I prefer BBEdit, its bigger brother, though, for its larger amount of functionality. Isn't free like TextWrangler, though – Thomas Tempelmann Dec 17 '08 at 15:26
2

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

Patrick Steil
  • 515
  • 4
  • 8
2

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

Community
  • 1
  • 1
Templar
  • 5,067
  • 7
  • 34
  • 39
2

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.

Ciantic
  • 6,064
  • 4
  • 54
  • 49
1

I'd go for bash + find + sed.

Rafał Dowgird
  • 43,216
  • 11
  • 77
  • 90
1

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.

Oliver Giesen
  • 9,129
  • 6
  • 46
  • 82
1

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.

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
1

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.

gdvalderrama
  • 713
  • 1
  • 17
  • 26
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
1

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.

CodePrinz
  • 445
  • 5
  • 8
1

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
jhw
  • 35
  • 1
  • 7
0

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.

victe
  • 516
  • 6
  • 8
0

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
0

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

Martin R-L
  • 4,039
  • 3
  • 28
  • 28