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:
1 |
grep -R " love" * |
with glark:
1 |
grep -r " love" * |
The first thing you’ll notice is the highlighting with glark:
Keyword and file highlighting is also possible with grep by using the color option:
1 |
grep -R --color=auto " love" * |
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:
1 |
glark -o ipv4 ipv6 * |
and the output:
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:
1 |
glark --after 20 expression * |
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:
1 2 3 4 5 6 7 8 9 |
file-color: bold reverse text-color: bold black on yellow context: 2 highlight: on verbose: false ignore-case: false quiet: yes word: false binary-files: without-match |
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.
Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.
Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress