11

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.

0

7 Answers 7

11

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.

5

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
2
  • 1
    Can you please be a bit more specific and describe how SublimeText solves the problem described in the question?
    – nohillside
    Feb 26, 2013 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).
    – Michael
    Feb 27, 2013 at 0:41
3

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.

3

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/

2

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
1

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

1
  • 1
    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, 2013 at 7:18
0

Take a look at my App Search and Replace. You can use it on multiple files or directories, safe your search/replace strings and you can use regular expressions.

3
  • How does this work - why is it better than other answers - please provide more than a link
    – mmmmmm
    May 15, 2014 at 23:10
  • 1
    Hi and welcome to Ask Different! While we don't mind developers recommending their products when they do solve problems being presented, we do ask you provide clear context for why your product is an answer and how it solves the problem. For more information on how Ask Different and the StackExchange Q&A format works please see our FAQ.
    – Ian C.
    May 16, 2014 at 1:11
  • I guess it has about the same features as the other tools recommended here. The difference is you have a GUI and you can save files containing your search/replace strings. It has all of the functionality nwinkler asked for. It can search and replace (also using regular expressions) across multiple files/nested directory structures and has (at least that's what i think) an easy to use GUI. May 30, 2014 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .