5

I'm trying to run watch, so that it shows me the output from my grep command, while I am running over files and changing things.

The usual way of doing this works fine, but lacks colors:

watch grep 'something' **/*

...and since I do want colors, I did this:

watch grep 'something' **/* --color=always

It's still ugly, because watch doesn't parse the escape codes that make the colors work. So then I found this:

watch --color grep 'something' **/* --color=always

And well... it doesn't work. The output is fragmented / mixed up in some weird way. It shows me only a single line for some reason, while without watch --color the grep outputs about 20.

How can I make watch work with colorized output from grep?


Isolated case:

Using the following:

mkdir grep_test; cd grep_test
echo "asdsad\nasdasd\nsaasdasd" > file1 
echo "test\n123\ntest" > file2

I create two files, and run the following command:

grep test * --color=always

Which works as expected, and outputs (with colors and whatnot):

file2:test
file2:test

But when I use watch:

watch --color 'grep test * --color=always'

It outputs nothing. Without the --color option it shows the right output, though without the ANSI escapes interpreted. I'm running this on Fedora 25, in tmux session with zsh inside.

Also, watch -v output is:

watch from procps-ng 3.3.10
0

1 Answer 1

7

It turns out that watch does work with color output. But specifically for grep you should make some workaround (according to grep --color adds ANSI code ESC[K - This can change displayed text)

watch --color "GREP_COLORS=ne grep --color=always .* *"
2
  • I tried using this, and the highlighted color is every single characters and subsequent lines after the grep search pattern. I am running on CentOS7 with tcsh interpreter
    – nabilishes
    Commented Dec 3, 2022 at 3:06
  • It was just an example. I case of matching only 'test' you should use modified pattern: watch --color "GREP_COLORS=ne grep --color=always test *" Commented Oct 25, 2023 at 16:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .