Replacing Sed/Awk With AmberJanuary 25, 2019
A couple of weeks ago I introduced the concept of replacing basic Unix equivalents with modern Rust alternatives with the example of Bat - today I want to introduce Amber, a modern alternative to awk and sed.
I recently migrated to Fastmail and switched my email address from firstname.lastname@example.org to email@example.com in an effort to avoid sharing too much information with Google and ensure people can contact me at the domains I own. Migrating email is hard, and will be the subject of another blog post I’m sure, but one specific area of difficulty was replacing every reference of firstname.lastname@example.org in my code bases. A lot of these references are innocuous - for example author emails in
package.json files, but a few are more important for example the email address listed in my CV. I wanted to catch them all in one fell swoop.
My code lives under a single directory called
~/projects. Whilst existing Linux utilities can handle finding and replacing recursively (either
awk would have worked), having used them before they aren’t the simplest of utilities to wrap your head around for this use case and I wanted something that would be easy for me to avoid making mistakes with. Enter amber! It made the whole process a breeze.
First I had to install amber which was straighforward. I grabbed the latest release from the Github releases page and then copied the
ambs (search only) and
ambr (search and replace) binaries to
/usr/bin. Then finding and replacing all the instances of my previous email address with my new one was as simple as the following commands.
cd ~/projects ambr email@example.com firstname.lastname@example.org
All that was left to do afterwards was commit the changes.
How hard would this have been to do using the alternatives I mentioned above? Well this looks reasonably hard according to StackOverflow. The top answer isn’t portable due to
sed differences on OSX, it fails on a git repositories as the replace doesn’t ignore the
.git folder, and the solution involves a complex chain of
xargs and pipes. How does that compare to amber? Well amber is portable, it ignores VCS locations (.git, .hg, .svn, .bzr) by default, and it has a far more ergonomic user interface. I’m looking forward to finding more modern Rust utilities to add to my toolbox.