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
.
Encode
s/Betty/Horsenator/g
s/Harold/Jimmybob/g
s/John/Farmboy/g
Decode
s/Horsenator/Betty/g
s/Jimmybob/Harold/g
s/Farmboy/John/g
Replace
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.
This article gives explains a few more basic sed
options that may help you get started.
You can find an example here to play with.
Top comments (2)
Note that
sed
you're using is BSD-style. On GNU, it'll still work (probably) but throw an error because the-i
flag doesn't take a filename.Good point!