3

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
  • Works fine on my system. Please edit your question and give us an example we can test. Try to reproduce the problem with as few simple input files as possible that we can set up on our machines and see if it works. – terdon Feb 21 '17 at 11:37
  • @terdon There you go. – Błażej Michalik Feb 21 '17 at 11:46
2

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 .* *"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.