Glark, an alternative to Grep

grep is a very useful tool to search or filter strings in order to look for files, parse useful info in log files and more.

glark is an alternative to grep, it has few features that grep does not such as complex expressions, Perl-compatible regular expressions, and excluding binary files. It also has a more fancy way of display results. It is described as follows in the manpage:

Similar to “grep”, “glark” offers: Perl-compatible regular expressions, color highlighting of matches, context around matches, complex expressions (“and” and “or”), grep output emulation, and automatic exclusion of non-text files. Its regular expressions should be familiar to persons experienced in Perl, Python, or Ruby. File may also be a list of files in the form of a path.

glark is not installed by default. To install it in Debian/Ubuntu/Mint:

sudo apt-get install glark

It does not appear to be available in Fedora and other RPM based distributions, so you’d have to download the source at http://sourceforge.net/projects/glark/ and build it.

The basic syntax is the same for grep and glark: grep string filenames

Let’s try to do a recursive search for ” love” in the Linux kernel.

With grep:


with glark:


The first thing you’ll notice is the highlighting with glark:

glark finds love in linux
Glark Output

Keyword and file highlighting is also possible with grep by using the color option:


The other thing you’ll soon notice is that it takes much longer to “find love” with glark than with grep.

grep takes 2m43s for the first run, and glark takes 4m20s. In subsequent runs (for example, if you want to search in files with a different keyword), grep takes 4 to 6 seconds whereas glark does not seem to take advantage of caching at all and still take 4m20s.

You’d rather use grep if you are searching thru a lot of file or data, but for smaller search the performance difference should not be noticeable.

Now, let’s use glark to search 2 string “ipv4” or “ipv6”. When the results are returned, glark will colorize the results and each search term will be highlighted in a different color.

Here’s the command line to achieve this:


and the output:

glark ipv4 / ipv6 search
Glark OR Search

Another interesting option of glark, if the ability to skip lines with the --before and --after  options. You can either use these as percentages or as absolute number of lines. For example if you want to skip the first 20 lines (e.g. copyright info)  in every files:


Glark should accept most options and expressions supported by grep and you can even force it to use grep style output with –grep

You can also have a ~/.glarkrc file to set default options for each use of glark, for example:


To conclude glark is an interesting tool. I like its highlighting features and that it can skip binary files, but the poor performance will make me stick to grep for now.

This blog post was mostly inspired by Is Glark a Better Grep? article on linux.com. You can find further info on glark manpage. If you are interested in yet another grep alternative, you can also checkout ack (ack-grep), a tool written in perl, “designed for programmers with large trees of heterogeneous source code. ” I’ve given it a quick try, and although not as fast as grep it’s much faster than glark for recursive searches.

Share this:

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK Pi 4C Plus
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
0 Comments
Khadas VIM4 SBC