grepis one of my go-to tools on any Linux box. But
grepisn't the only tool in town. If you want to try something a bit different, check out glark a
grepalternative that might might be better in some situations.
What is glark? Basically, it's a utility that's similar to grep, but it has a few features that grep does not. This includes complex expressions, Perl-compatible regular expressions, and excluding binary files. It also makes showing contextual lines a bit easier. Let's take a look.
I installed glark (yes, annoyingly it's yet another *nix utility that has no initial cap) on Linux Mint 11. Just grab it with
apt-get install glarkand you should be good to go.
Simple searches work the same way as with
glark string filenames. So it's pretty much a drop-in replacement for those.
But you're interested in what makes
glarkspecial. So let's start with a complex expression, where you're looking for this or that term:
glark -r -o thing1 thing2 *
This will search the current directory and subdirectories for "thing1" or "thing2." When the results are returned,
glarkwill colorize the results and each search term will be highlighted in a different color. So if you search for, say "Mozilla" and "Firefox," you'll see the terms in different colors.
You can also use this to see if something matches within a few lines of another term. Here's an example:
glark --and=3 -o Mozilla Firefox -o ID LXDE *
This was a search I was using in my directory of Linux.com stories that I've edited. I used three terms I knew were in one story, and one term I knew wouldn't be. You can also just use the
--andoption to spot two terms within X number of lines of each other, like so:
glark --and=3 term1 term2
That way, both terms must be present.
You'll note the
--andoption is a bit simpler than grep's context line options. However, glark tries to stay compatible with grep, so it also supports the
-Coptions from grep.
Miss the grep output format? You can tell glark to use grep format with the
Most, if not all, GNU grep options should work with
Before and AfterIf you need to search through the beginning or end of a file,
--afteroptions (short versions,
-a). You can use these as percentages or as absolute number of lines. For instance:
glark -a 20 expression *
That will find instances of expression after line 20 in a file.
The glark Configuration FileNote that you can have a
~/.glarkrcthat will set common options for each use of glark (unless overridden at the command line). The man page for
glarkdoes include some examples, like so:
after-context: 1 before-context: 6 context: 5 file-color: blue on yellow highlight: off ignore-case: false quiet: yes text-color: bold reverse line-number-color: bold verbose: false grep: true
Just put that in your
~/.glarkrcand customize it to your heart's content. Note that I've set mine to
grep: falseand added the
binary-files: without-matchoption. You'll definitely want the quiet option to suppress all the notes about directories, etc. See the man page for more options. It's probably a good idea to spend about 10 minutes on setting up a configuration file.
Final ThoughtsOne thing that I have noticed is that
glarkdoesn't seem as fast as
grep. When I do a recursive search through a bunch of directories containing (mostly) HTML files, I seem to get results a lot faster with
grep. This is not terribly important for most of the stuff I do with either utility. However, if you're doing something where performance is a major factor, then you may want to see if
grepfits the bill better.
Is glark "better" than grep? It depends entirely on what you're doing. It has a few features that give it an edge over grep, and I think it's very much worth trying out if you've never given it a shot.