Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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