If you are even a casual Linux user, you probably know how to use
grep. Even if you aren’t a regular expression guru, it is easy to use grep to search for lines in a file that match anything from simple strings to complex patterns. Of course, grep is fine for looking, but what if you want to find things and change them. Maybe you want to change each instance of “HackADay” to “Hackaday,” for example. You might use
sed, but it is somewhat hard to use. You could use
awk, but as a general-purpose language, it seems a bit of overkill for such a simple and common task. That’s the idea behind ripgrep which actually has the command name
rg. Using rg, you can do things that grep can do using more modern regular expressions and also do replacements.
A Note on Installing Ripgrep
Your best bet is to get ripgrep from your repositories. When I tried running KDE Neon, it helpfully told me that I could install a version using apt or take a Snap version that was newer. I usually hate installing a snap, but I did anyway. It informed me that I had to add –classic to the install line because ripgrep could affect files outside the Snap sandbox. Since the whole purpose of the program is to change files, I didn’t think that was too surprising, so I did the install.
Continue reading “Linux Fu: Global Search And Replace With Ripgrep”
Regular expressions — the things you feed to programs like
grep — are a bit like riding a bike. It seems impossible until you learn to do it, and then it’s easy. Part of their bad reputation is because they use a very concise and abbreviated syntax that alarms people. To help people who don’t use regular expressions every day, I created a tool that lets you write them in something a little closer to plain English. Actually, I’ve written several versions of this over the years, but this incarnation that targets
grep is the latest. Unlike some previous versions, this time I did it all using Bash.
Those who don’t know regular expressions might freak out when they see something like:
How long does it take to figure out what that does? What if you could write that in a more literate way? For example:
digit repeat 5 \
- digit repeat 4 \
Not as fast to type, sure. But you can probably deduce what it does: it reads US Zipcodes.
I’ve found that some of the most popular tools I’ve created over the years are ones that I don’t need myself. I’m sure you’ve had that experience, too. You know how to operate a computer, but you create a menu system for people who don’t and they love it. That’s how it is with this tool. You might not need it, but there’s a good chance you know someone who does. Along the way, the code uses some interesting features of Bash, so even if you don’t want to be verbose with your regular expressions, you might pick up a trick or two.
Continue reading “Linux Fu: Literate Regular Expressions”