leah blogs

July 2013

03jul2013 · Summer of Scripts: hl

I generally dislike unasked-for color output of various tools, especially grep (grep is there to “print lines matching a pattern” and not confuse me with highlighted terms). That’s why I usually disable such things.

However, sometimes I have to debug ugly or very verbose output and want to highlight some words. For these cases, I (ab)use the --color function of grep in a zsh function hl:

hl() {
  egrep --color=always -e '' -e${^*}

The purpose of grep is defeated by searching for the empty string, so all lines will be shown, but the matches still are being highlighted:

% head -c4k /dev/urandom | xxd | hl f00
0000f20: 3205 ab77 a1a0 5eeb c5e6 2c6e 42d6 1195  2..w..^...,nB...
0000f30: 7b0a 5ad0 3d6f 79ef 0dfc f0aa 6819 d168  {.Z.=oy.....h..h
0000f40: 6aea 3280 38f2 e437 54d9 372d 1b4f 5f00  j.2.8..7T.7-.O_.
0000f50: 6a90 d459 afc3 da2b b0bb 2349 b5a5 dc44  j..Y...+..#I...D
0000f60: a1e8 d21d f8ba db6a 0908 eb1c b2e3 ad48  .......j.......H
0000f70: c871 8dde a3ee 019c 6053 2a4a b205 2136  .q......`S*J..!6

Also note that several patterns can be given on the command line:

% objdump -d =grep | hl push pop | less -R

NP: Die Schnitter—Widerstand

Copyright © 2004–2022