Formatting the output of grep when matching on multiple files

grep

When searching on multiple files, the output of grep looks something like this:

{FILE}:{MATCH}

This is a problem since if I double-click on the {FILE} portion, I will at least also select (and copy) the : and possibly some of the match. How can I get grep to instead format its output something like this:

{FILE} : {MATCH}

Basically, I want to insert a space before the column. (and optionally one after)

Best Answer

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.

Related Question