Skip to main content
added 90 characters in body; added 9 characters in body
Source Link
jlliagre
  • 62.5k
  • 11
  • 124
  • 162

None of the answers given so far do provide a portable solution.

Here is a portable1 shell function I already posted in a closed as duplicate question that doesn't require non standard tools or non standard extensions provided with perl, ack, ggrep, gsed, bash and the likes but only needs a POSIX shell and the POSIX mandatory utilities sed and printf:

grepc() { pattern=$1 shift esc=$(printf "\033") sed 's"'"$pattern"'"'$esc'[32m&'$esc'[0m"g' "$@" } 

You can use it that way:

grepc string_to_search [file ...] 

The highlight color can be adjusted by using one of these codes in the sed command argument (32m being green here):

30m black 31m red 33m yellow 34m blue 35m magenta 36m cyan 37m white 7m reverse video 

1 As long as your terminal supports ANSI colors escape sequences.

None of the answers given so far do provide a portable solution.

Here is a shell function I already posted in a closed as duplicate question that doesn't require non standard tools or non standard extensions provided with perl, ack, ggrep, gsed, bash and the likes but only needs a POSIX shell and the POSIX mandatory utilities sed and printf:

grepc() { pattern=$1 shift esc=$(printf "\033") sed 's"'"$pattern"'"'$esc'[32m&'$esc'[0m"g' "$@" } 

You can use it that way:

grepc string_to_search [file ...] 

The highlight color can be adjusted by using one of these codes in the sed command argument (32m being green here):

30m black 31m red 33m yellow 34m blue 35m magenta 36m cyan 37m white 7m reverse video 

None of the answers given so far do provide a portable solution.

Here is a portable1 shell function I already posted in a closed as duplicate question that doesn't require non standard tools or non standard extensions provided with perl, ack, ggrep, gsed, bash and the likes but only needs a POSIX shell and the POSIX mandatory utilities sed and printf:

grepc() { pattern=$1 shift esc=$(printf "\033") sed 's"'"$pattern"'"'$esc'[32m&'$esc'[0m"g' "$@" } 

You can use it that way:

grepc string_to_search [file ...] 

The highlight color can be adjusted by using one of these codes in the sed command argument (32m being green here):

30m black 31m red 33m yellow 34m blue 35m magenta 36m cyan 37m white 7m reverse video 

1 As long as your terminal supports ANSI colors escape sequences.

Source Link
jlliagre
  • 62.5k
  • 11
  • 124
  • 162

None of the answers given so far do provide a portable solution.

Here is a shell function I already posted in a closed as duplicate question that doesn't require non standard tools or non standard extensions provided with perl, ack, ggrep, gsed, bash and the likes but only needs a POSIX shell and the POSIX mandatory utilities sed and printf:

grepc() { pattern=$1 shift esc=$(printf "\033") sed 's"'"$pattern"'"'$esc'[32m&'$esc'[0m"g' "$@" } 

You can use it that way:

grepc string_to_search [file ...] 

The highlight color can be adjusted by using one of these codes in the sed command argument (32m being green here):

30m black 31m red 33m yellow 34m blue 35m magenta 36m cyan 37m white 7m reverse video