Aug 102011

By Joe ‘Zonker’ Brockmeier

GNU grep is one of my go-to tools on any Linux box. But grep isn’t the only tool in town. If you want to try something a bit different, check out glark a grep alternative 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 glark and you should be good to go. Simple searches work the same way as with grep: glark string filenames. So it’s pretty much a drop-in replacement for those. But you’re interested in what makes glark special. 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, glark will 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 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 –and option 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 –and option is a bit simpler than grep’s context line options. However, glark tries to stay compatible with grep, so it also supports the -A, -B and -C options from grep. Miss the grep output format? You can tell glark to use grep format with the –grep option. Most, if not all, GNU grep options should work with glark.

Before and After

If you need to search through the beginning or end of a file, glark has the –before and –after options (short versions, -b and -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 File

Note that you can have a ~/.glarkrc that will set common options for each use of glark (unless overridden at the command line). The man page for glark does 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 ~/.glarkrc and customize it to your heart’s content. Note that I’ve set mine to grep: false and added the binary-files: without-match option. 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 Thoughts

One thing that I have noticed is that glark doesn’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 grep fits 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.


One more thing, Glark can be installed on a Cent/Redhat server using

gem install glark
Share This!
 Posted by at 9:45 am

Sorry, the comment form is closed at this time.