Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

igneousc

macrumors 6502
Original poster
May 13, 2011
292
344
I have a text document that I write every day and need to find and replace certain text before I'm done with it. For a while now I just paste the text into bbedit, and find and replace those sections that occur in that document that day and I'm done. But Im trying to figure out the easiest way to automate this so I can do it all with a single click.

The sections of text that I need to replace each day are roughly 20 sections of text, so something like 1, 2, 3, 4, etc.
So I would search for "1" and replace with a blank spot
search for "2" and replace with a blank spot
search for "4" and replace with a blank spot
every day is different, but its always one of those same sections of text like 1,2, 3, 4 etc.

I tried to figure it out in shortcuts, as well as the bbedit menu, but couldn't figure anything out that worked, and had a hard time finding a youtube tutorial showing me how to do this. Its not super complicated, so I feel like I'm not searching for the right terms or something. Any tips?
 

chabig

macrumors G4
Sep 6, 2002
11,449
9,320
I'm sure BBEdit can do that all in one move using GREP. I recommend downloading and referring to the BBEdit User Manual for help. I'm not GREP smart, but others here probably are.
 

igneousc

macrumors 6502
Original poster
May 13, 2011
292
344
Did you search for "macos batch find and replace"?

I don't know of anything but perhaps the terminology will narrow down the results.
yeah I did a couple similar searches like that and it leads to a lot of people looking to find and replace across multiple files
 

igneousc

macrumors 6502
Original poster
May 13, 2011
292
344
I'm sure BBEdit can do that all in one move using GREP. I recommend downloading and referring to the BBEdit User Manual for help. I'm not GREP smart, but others here probably are.
yea right now I have them mostly saved as patterns in the find and replace box, so I just run them one by one. My worry is that some solution will require me to get the paid version of bbedit and its probably not worth me spending $40 a year for
 

TechnoMonk

macrumors 68030
Oct 15, 2022
2,606
4,115
I use shell script for text manipulation in my datasets. A simple shell script or python can easily do the job. Ability to run Linux/Unix type libraries is one of the good things I like about Macs.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,998
8,887
A sea of green
You might look at the 'sed' command to peform multiple edits on multiple patterns.

You might also look at inverting the problem. That is, instead of looking at it as a text file with multiple patterns that are replaced, consider treating it as a series of text fragments in individual files, which are ultimately concatenated into a single composite text document. Each of the individual replaceable pieces sits in its own file, as does each constant piece. Write each replaceable piece using the 'echo' command with redirected stdout, e.g. echo "text you want" >piece-4.txt.

The 'echo' command can write any text to any file. It will overwrite if you use the > redirection operator, or it will append if you use >>. Those are shell operators, so you will need to use them in a shell script.

After all the fixed and variable pieces are in their proper files, the 'cat' command can concatenate them, and again you use one of the redirection operators to write the composite output to a file. If one of the pieces needs to appear more than once, simply give its filename again at the proper point in the sequence.

Although this might seem complex at first glance, it has the advantage of you not needing to learn any reg-exes, nor how to replace things with 'sed' or whatever. That is, each individual step is simple to understand and work with, and the result is obtained by adding together a bunch of simple parts.
 

Basic75

macrumors 68020
May 17, 2011
2,101
2,448
Europe
To the people suggesting grep(1), that doesn't replace, it only searches. You can use sed(1) or awk(1), or a scripting language to do complex text manipulations.
 
  • Like
Reactions: TechnoMonk
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.