Inspired by pudge over at use.perl, here's a short little script you can use to highlight pattern matches in input data.
usage: hl [ -c colour ] [ -x ] pattern [ file... ] [ < input ]
You can use capturing parens in your pattern. In that case, you can supply multiple attributes separated by commas, which will be used to individually colour the submatches.
-x will supress lines without matches.
Update: fixed massive offset calculation bug, hugely simplified the colourizing routine.
Due to the semantics of the @- and @+ arrays, my first stab was a horrible monster and incredibly difficult to debug, far harder to write than it promised to be. The special entries at index 0 indicating the start and end of the entire match required terrible contortions to take into account.
And, surprise surprise, the code was buggy.
In fixing my bug, I realized that the proper special case looked almost like a common case. And then I realized that by appending a phantom zero-length match and changing index 0 to instead signify a phantom zero-length 0th match, both special cases disappear.
Lesson: when implementing the semantics turns your brain to mush, change the semantics.
For a history of the code, look at aforementioned use.perl thread. |