Hello,

On 13/08/17 12:45 PM, Marcel Partap wrote:
>> One way:
>> grep <arguments> | sed "s/:/ /"
> Thanks, obvious approach, but loses colour 😁

Few things:

First,
you can force grep to output color with:

   grep --color=always <argument> | sed 's/:/ /'


Second,
If you grep on a shell-glob (e.g. "*.txt"),
you might want to add "-H -n" to force grep
to always output the filename and the line number.
Otherwise, if the glob matches a single file,
grep by default will not output the filename/line number -
and the 'sed' will not do what you wanted.

   grep --color=always -Hn <argument> *.txt


Third,
This assumes filenames do not contain the character ':'
(which is probably a valid assumptions most of the time, but not always).
To be more robust, you can use GNU grep's "-Z" option, which outputs
a NUL after the filename, the GNU sed to replace the NUL with something
else.
Example:

  $ grep --color=always -HZn Deb /etc/motd | sed 's/\x00/ === /'
  /etc/motd === 2:The programs included with the Debian GNU/Linux
  /etc/motd === 6:Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY



Fourth,
To easily automate all of that, you can create
a tiny shell function (put it in ~/.bashrc or ~/.bash_aliases, etc):

  nicegrep() { grep --color=always -HZn "$@" | sed 's/\x00/ === /' ; }

Then run:

  nicegrep Deb /etc/motd




regards,
 - assaf




Reply via email to