bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#28081: way to change line number separator from colon to space?


From: Assaf Gordon
Subject: bug#28081: way to change line number separator from colon to space?
Date: Sun, 13 Aug 2017 13:05:42 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1

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

[Prev in Thread] Current Thread [Next in Thread]