Changing terminal size affects the output of grep inside a watch command

1

If I run a command like:

watch "ps -ef | grep '^foo.*bar'"

and the matching output line of ps -ef is a long line like

foo <lots and lots of text here> bar

whether this line is output is affected by the terminal size. For example, if the terminal is full screen and this line fits on the screen, then the output will be shown in watch. However, if I resize the window to half the screen size so that the bar is truncated from the screen, then the grep line entirely disappears (presumably the line is truncated before it gets to grep in the pipeline). This is a problem because I'm trying to extract a portion of this line for use later in the pipeline.

Is there any solution to this to prevent the terminal size from affecting the output of grep within a watch command?

jonderry

Posted 2011-04-28T21:22:22.910

Reputation: 837

Answers

2

It's not grep, it's ps. Add the -ww options (ps -efww) to tell it to always output full lines.

user1686

Posted 2011-04-28T21:22:22.910

Reputation: 283 655

1

I don't know what the right solutions is, but this seems to fix the problem,

watch "COLUMNS=160 ps -ef | grep '^foo.*bar'"

at the expense of having the lines from ps folded. Change "160" to be long enough for the longest "foo.*bar" line you expect to see.

garyjohn

Posted 2011-04-28T21:22:22.910

Reputation: 29 085