16
Find and Replace a Bunch
Reposted from original article
Say I'm writing a journal and want to make sure I'm not leaking names. I'd like to change it consistently so it's anonymous, but still makes sense.
Instead of this
John force-pushed to `master` for some reason. Harold didn't pull before he
added his fix and Betty ended up cherry-picking into yet another branch. To
avoid this, next time we could...
I'd like this
Farmboy force-pushed to `master` for some reason. Jimmybob didn't pull before
he added his fix and Horsenator ended up cherry-picking into yet another
branch. To avoid this, next time we could...
Find-and-replace is what I want to do, but consistently and repeatedly (and quickly!). sed
is the tool for the job.
First, define the replacements in files named encode
and decode
.
s/Betty/Horsenator/g
s/Harold/Jimmybob/g
s/John/Farmboy/g
s/Horsenator/Betty/g
s/Jimmybob/Harold/g
s/Farmboy/John/g
Then to replace, we use one of
sed -i "" -f encode journal.md
sed -i "" -f decode journal.md
Obviously, we don't want to keep the encode/decode files with the output, but you get the idea. You can also send a whole bunch of files through sed
with this set up. Maybe you could add a build step that cleans up content or something.
You can find an example here to play with.