Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm looking for an OS X tool which allows me to search and replace a search string in multiple files. The requirements are the following:

  • Search and Replace
  • Search by regular expression
  • Search across multiple files
  • Search across nested directory structures
  • Replace expression can use values from the search (Regular Expression)
  • The tool has an easy to use user interface
  • Preferably free

I know that some text editors like BBEdit have this functionality, but I would prefer a lightweight tool that doesn't require me to open the files for editing first.

If there aren't any tools to match these requirements, I would also consider a command line alternative. I know that perl and sed support something like this - a solution that I can define either as a Bash alias or function would be welcome in this case.

share|improve this question
add comment

6 Answers

I typically use TextMate:

TextMate Search and Replace dialog

TextMate (1) is/was a buy product, but TextMate 2 is currently under active openSource development on gitHub and free to use.

share|improve this answer
add comment

Sublime Text is what I use, it's great!

screenshot of find and replace

Sublime Text:

  • is free to evaluate (no time limit imposed)
  • can search and replace
  • can search by regular expression
  • can search across multiple files
  • can search across nested directory structures
  • can use values from the search in replace expression
  • has an easy to use user interface
share|improve this answer
    
Can you please be a bit more specific and describe how SublimeText solves the problem described in the question? –  patrix Feb 26 '13 at 10:29
    
Their website (link provided tells you everything) but... It is free to evaluate (no time limit imposed) Search and Replace - YES. Search by regular expression - YES. Search across multiple files - YES. Search across nested directory structures - YES. Replace expression can use values from the search (Regular Expression) - YES. The tool has an easy to use user interface - YES. Preferably free - YES (sort of). –  Mike Feb 27 '13 at 0:41
add comment

You don't have to open the files with BBEdit. You just point it to a folder. It's a very powerful tool, one that I've often used with large numbers of HTML files.

share|improve this answer
add comment

In addition to the other solutions using a text editor, I have found FAR - Find and Replace, a multi-platform find and replace tool. It's not the prettiest one, but it looks like it does what I'm looking for.

http://findandreplace.sourceforge.net/

share|improve this answer
add comment

I often use Ruby scripts like this:

Dir["**/*.txt"].each { |f|
  IO.write(f, IO.read(f).gsub(/search/, "replace"))
}

Or you can use gets(nil) with -i:

ruby -i -e 'print gets(nil).gsub(/search/, "replace")' *.txt

If you install Bash 4 and enable globstar, you can use ** in shells as well:

sed -i '' 's/search/replace/g' **/*.txt
share|improve this answer
add comment

This is exactly sed was written and it matches all of your requirements, except the learning curve is a bit steep.

For a more gentle learning curve, try TextWrangler from BareBones. It uses the BBEdit text engine and, I believe, has the full search & replace functionality of its big brother

share|improve this answer
    
I'm familiar with some bash programming and scripting, but have never done anything advanced with sed. Can you provide an example for how to use sed to replace text in a series of files, like in the (currently) above ruby answer? –  nwinkler Feb 28 '13 at 7:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.