Any accessible text editor on mac that has multiple text replacement across multiple files?

By Igna Triay, 28 October, 2023

Forum
macOS and Mac Apps

Is there any text editor that can replace find and replace something across multiple files at once?
I have 121 files, which all have the same 3 values, 1000, 100, and 50. I need to replace these 3 values with different ones, across all the files, however opening a file at a time, and manually replacing each value, is time consuming, to say the least. So is there a text editor out there that will allow me to do this? I tried sublime text editor, which has the capability of doing this, but sadly, it is not accessible at all.
Thanks for any help.

Options

Comments

By João Santos on Friday, November 24, 2023 - 20:31

The most practical way I can think of is to use command-line utilities like tr, sed, awk, or perl, with tr being the simplest yet least powerful and perl being an actual programming language.

To change 'John Doe' to 'Jack Doe' in a bunch of plain text files residing in a specific folder, do the following:

  1. Browse to the folder using Finder;
  2. Press Command+Option+P in the finder window to activate the Path Bar;
  3. Navigate to the Path Bar using the VO cursor and interact with it;
  4. Navigate to the rightmost element of the Path Bar using the VO cursor, activate the context menu, and choose Open in Terminal;
  5. In the terminal window that opens, type: perl -pi.bak -ne 's/\bJohn(?=\s+Doe)\b/Jack/g' *.txt.

Breakdown of the command above:

  • The perl command invokes the Perl interpreter which ships with MacOS by default;
  • The p switch makes Perl produce output after processing the input;
  • the i.bak switch causes Perl to produce backup files with a .bak extension just in case you make a mistake;
  • The n switch causes perl to wrap the program in a while loop that iterates once per input line;
  • The e switch causes Perl to execute the code provided in the command-line instead of reading it from a file;
  • The single quotes around the regular expression prevent the shell from interpreting anything inside, passing it to Perl as-is;
  • the *.txt argument expands to all the files with the .txt extension in the folder, causing Perl to execute the same code for all of them.

As for the regular expression inside the single quotes, it means the following:

  • The s///g statement matches the current line against the expression between the first two slashes and replaces all matches with the string between the last two slashes, you can provide multiple statements like this separated by a semicolon;
  • The \b escape sequence means to match a word boundary in the input string;
  • The (?=) is a look-ahead subexpression which tells Perl to match anything followed by its contents of the subexpression but without replacing it;
  • The \s escape sequence means to match any white-space character;
  • The + means one or more occurrences.

You can read more about Perl's regular expressions in the official Perl documentation (which unfortunately I cannot link to because I'm considered a spam bot). These regular expressions are actually a de-facto standard used in many programming languages and even text editors, so learning them never hurts.

If you need further assistance, just reply to the thread.

By Michael on Friday, November 24, 2023 - 20:31

From my experience, Visual Studio Code provides good accessibility with VoiceOver. You can use the command+shift+f keyboard command to search across all files in the current folder. There is a replace option that can be toggled and you can then enter the replacement text. You can press command+enter (instead of just enter, which replaces one occurrence) to replace all.

You can find the Visual Studio code for Mac download page quite easily (for me it was the first option) and from that website, you can directly download the app.

Good luck!
Michael

By Johann on Friday, November 24, 2023 - 20:31

I think TextMate or CotEditor might have that feature

By Jason White on Friday, November 24, 2023 - 20:31

Perl is exactly what I use for this purpose. You can write a script if you want to automate the replacement process - useful if you need to do it repeatedly.

By PaulMartz on Friday, November 24, 2023 - 20:31

I hate even suggesting it, as Igna Triay's post indicates they have no experience with the shell. And a task like this is not a beginner's task. But it's exactly the kind of thing these tools were designed to handle. You might be able to perform a substitution with a single find command that executes a sed script on each file, then edit the command for subsequent substitutions.

By Igna Triay on Friday, November 24, 2023 - 20:31

I have messed about with the terminal quite a bit, but not that much, but will try the pearl script.