Question

Preferably free tools if possible.

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

Was it helpful?

Solution

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

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

OTHER TIPS

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/" "{}" ";"

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

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.

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

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.

Under Windows, I used to like WinGrep

Under Ubuntu, I use Regexxer.

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

For Mac OS X, TextWrangler does the job.

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

I'd go for bash + find + sed.

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.

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

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.

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

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.

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.

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

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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top