ps ax | grep java with no wrapping in the terminal?

Is there a way to execute ps ax | grep java without it wrapping on the terminal?

-
which OS? Which terminal app? –  keithosu Sep 2 '09 at 18:32
OS X Terminal specifically, but I've bumped into this on other platforms. –  Noah Campbell Sep 2 '09 at 19:15

For me, ps doesn't wrap unless I do:

ps axw


However, you can set the screen width like this to truncate the output (but it won't override -w):

ps ax --width=80


You can also use the o (or -o or --format) option to include only columns that you are interested in, change or eliminate column headers and set the width of each column individually*. See man ps and search for "user-defined format" (multiple occurrences).

* setting a column width smaller than normal may change the way the contents or displayed or may not have an affect. For example, "args:20" doesn't truncate the output (unless it's not the last column) and "user:5" causes usernames longer than five characters to be displayed as the UID number.

-
ps ax doesn't wrap, agreed. However, the use of grep causes it to wrap. –  Noah Campbell Sep 2 '09 at 19:17
@noah: since when? i pipe ps into grep all the time, and it doesn't wrap. if either your ps or your grep are wrapping lines, that's a bug. it's also very unlikely. the culprit is your terminal emulator. –  cas Sep 2 '09 at 21:55
@Craig Sanders: See man ps in the section "STANDARD FORMAT SPECIFIERS" under "args" and "comm" for a discussion of their behavior during redirection. It says "when output is redirected (piped) into a file or another command, the output width is undefined. (it may be 80, unlimited, determined by the TERM variable, and so on)" –  Dennis Williamson Sep 2 '09 at 23:50
@dennis: yes, i know that. it doesn't say anything at all about wrapping lines. and as i said earlier, neither ps nor grep wrap lines, nor should they. –  cas Sep 3 '09 at 8:06
@Craig Sanders: So what happens for you when text exceeds the line length of your terminal? How would you propose that Noah deal with it? –  Dennis Williamson Sep 3 '09 at 9:20

I am not able to comment on Dennis Williamson's answer (maybe I have too few points for that); that's why I am writing a new answer.

You can do

ps ax --width=$COLUMNS  to get the output width adjusted according to the current terminal size. If your favorite shell does not support COLUMNS environmental variable, you can use stty: ps ax --width=$(stty -a | grep 'columns [0-9]*;' | sed 's|.*columns $$[0-9]*$$.*|\1|')


or more specifically (using single awk but making stronger assumption on the stty -a output format):

ps -axw --width=$(stty -a | awk '/columns/ { printf "%d",$7 }')


All this is quite a bit of typing, so you might want to consider making the above an alias or a shell function... :-) The above maybe needs to be adjusted for your operating system (I cannot test now, I am not using OS X but Linux). I have the following output for stty -a:

speed 38400 baud; rows 42; columns 178; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
flush = ^O; min = 1; time = 0;
...

-