Outline grep

Featureful tool for searching in indentation-structured text files.

Inspired by ogrep by Matt Brubeck. That ogrep is compact and beautiful, but not featureful.

See also ogrep — port of this tool written in Python (to be truly, it was first).

Brief

ogrep is much like grep, both can search for matches and display their context. But context in grep is “N lines before/after match”, and in ogrep it is “lines above matched one with lower indentation”.

Let me explain. I use this tool mostly when working with GN build files, so I'll use some large BUILD.gn file as an example. Usual task is to search for source file name and understand which target includes this file and under which conditions.

Let's find mentions of “arena.cc” file:

```

grep arena.cc BUILD.gn

  "base/arena.cc",

```

Ok, now we now that our file is here, but don't know target. Let's ask for some context:

```

grep -C2 arena.cc BUILD.gn

  "base/address_tracker_linux.cc",
  "base/address_tracker_linux.h",
  "base/arena.cc",
  "base/arena.h",
  "base/backoff_entry.cc",

```

Nope, not that useful. Let's try ogrep:

ogrep arena.cc BUILD.gn 102: component("net") { 385: if (!is_nacl) { 386: sources += [ 409: "base/arena.cc",

Now that's useful! We immediately know that file in included into “net“ target under “!is_nacl” condition.

It is even better, because ogrep can use colors, here is a picture:

Options

There are plently of available options, run with --help to list them.

Tool is useful not only for strict indentation-based files (like Python source) or GN build files, but for wide range of text files, because even not-indentation based ones are usually formatted for convenience.

There are even some C-related hacks built-in.

Here is brief feature list:

Searching in directory

Integration with external tools

otool in intended to search in single file only. And it is not so fast to be used for searching through many files. But you can integrate it with other search tools like this:

grep -l cache_used -r . --include='*.cc' | xargs -n1 ogrep --print-filename cache_used

Builtin git grep support

ogrep has builtin integration with git grep: when -g option is given, second argument is passed to git grep as path specification. All relevant options (-w, -i, etc.) are also passed to git grep automatically, --print-filename is forced.

ogrep -g cache_used '*.cc'