Formatting the output of grep when matching on multiple files

grep -T will work 7/8ths of the time.

% for f in a ab abc abcd abcde abcdef abcdefg abcdefgh; do echo pattern > $f; done
% grep -T pattern *
a      :pattern
ab     :pattern
abc    :pattern
abcd   :pattern
abcde  :pattern
abcdef :pattern
abcdefg:pattern
abcdefgh       :pattern

From the GNU grep manual:

-T --initial-tab

Make sure that the first character of actual line content lies on a tab stop, so that the alignment of tabs looks normal. This is useful with options that prefix their output to the actual content: -H, -n, and -b. In order to improve the probability that lines from a single file will all start at the same column, this also causes the line number and byte offset (if present) to be printed in a minimum-size field width.


This cannot be done by grep itself as far as I can tell. Assuming your filenames don't have a : in them:

grep ... | sed 's/:/ : /'

Only the first : will be padded.

Of course, you can tell grep to only print filenames:

grep -l ...

What constitutes a word as far as selection with double-clicking is concerned is terminal (and/or X toolkit) dependant and for some terminals, customizable.

For xterm, characters are organised in classes (letters, spaces...) and double clicking selects adjacent characters of the same class.

The default is described there. In that default, : is not in the same class as / itself not in the same class as letters or digits.

Some systems change that default by providing a resource file for xterm like /etc/X11/app-defaults/XTerm.

On a Linux Mint system, in there, I find:

! Here is a pattern that is useful for double-clicking on a URL:
*charClass: 33:48,35:48,37-38:48,43-47:48,58:48,61:48,63-64:48,95:48,126:48

That puts characters 58 (:) and 47 in the same class as letters and digits (48).

What you could do is change that to leave : in its own class via your own resource file.

For instance, if you add:

XTerm*charClass: 33:48,35:48,37-38:48,43-47:48,61:48,63-64:48,95:48,126:48

to your ~/".Xdefaults-$(uname -n)", then double clicking on the file name will stop at the :.

You can experiment with it with:

xterm -cc 33:48,35:48,37-38:48,43-47:48,61:48,63-64:48,95:48,126:48

You can also define a different selection method on triple or quadruple click as a regular expression. For instance

XTerm*on3Clicks: regex [^:]+

Would select sequences of non-colon characters upon triple-click.

Tags:

Grep