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.
By Igna Triay, 28 October, 2023
Forum
macOS and Mac Apps
Comments
Command-line utilities
The most practical way I can think of is to use command-line utilities like
tr
,sed
,awk
, orperl
, withtr
being the simplest yet least powerful andperl
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:
perl -pi.bak -ne 's/\bJohn(?=\s+Doe)\b/Jack/g' *.txt
.Breakdown of the command above:
perl
command invokes the Perl interpreter which ships with MacOS by default;p
switch makes Perl produce output after processing the input;i.bak
switch causes Perl to produce backup files with a.bak
extension just in case you make a mistake;n
switch causes perl to wrap the program in a while loop that iterates once per input line;e
switch causes Perl to execute the code provided in the command-line instead of reading it from a file;*.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:
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;\b
escape sequence means to match a word boundary in the input string;(?=)
is a look-ahead subexpression which tells Perl to match anything followed by its contents of the subexpression but without replacing it;\s
escape sequence means to match any white-space character;+
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.
VS Code is accessible.
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
Try
I think TextMate or CotEditor might have that feature
Perl
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.
Ditto command line utilities
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.
I have messed around with terminal
I have messed about with the terminal quite a bit, but not that much, but will try the pearl script.