bashshell

How can I limit output to the terminal width


When I use pstree I see that lines only go up to the terminal width (that is, no word wrap), but when I grep the output, it does wrap. What function is it using to change this behavior?

bash$ pstree
\--= 76211 _spotlight /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker MDSImporte
bash$ pstree | grep MDSImporte
\--= 76211 _spotlight /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker MDSImporterWorker com.apple.Spotlight.ImporterWorker.89

Solution

  • pstree seems to think that you don't want wrapped output, thus it asks the terminal about its width and outputs just as much. top and ps behave similar.

    You can avoid this by piping the output through cat:

    pstree | cat
    

    Edit: Ah, I see that you want not avoid it, but add the chopping.

    An easy way is piping the output of your command through less -S (or less --chop-long-lines, more verbosely). (You may want to combine this with some other options, see the manual page, depending on your preferences).

    pstree | grep MDSImporte | less -SEX
    

    will show your the lines cut off at terminal size.