bash 如何将输出限制为终端宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6820384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I limit output to the terminal width
提问by nachocab
When I use pstreeI see that lines only go up to the terminal width (that is, no word wrap), but when I grepthe output, it does wrap. What function is it using to change this behavior?
当我使用时,pstree我看到行仅达到终端宽度(即没有自动换行),但是当我grep输出时,它会自动换行。它使用什么功能来改变这种行为?
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
回答by Pa?lo Ebermann
pstreeseems to think that you don't want wrapped output, thus it asks the terminal about its width and outputs just as much. topand psbehave similar.
pstree似乎认为您不想要包装输出,因此它会询问终端有关其宽度和输出的信息。top并且ps行为相似。
You can avoid this by piping the output through cat:
您可以通过管道输出来避免这种情况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).
一种简单的方法是通过less -S(或者less --chop-long-lines更详细地)通过管道传输命令的输出。(您可能希望将此与其他一些选项结合使用,请参阅手册页,具体取决于您的偏好)。
pstree | grep MDSImporte | less -SEX
will show your the lines cut off at terminal size.
将显示您在终端尺寸处切断的线条。
回答by Rob Davis
pstreemust be checking to see if it's writing to a terminal, and if so it queries the terminal for its column width and then limits the output accordingly. You could do something similar:
pstree必须检查它是否正在写入终端,如果是,它会查询终端的列宽,然后相应地限制输出。你可以做类似的事情:
WIDTH=`stty size | cut -d ' ' -f 2` # Get terminal's character width
pstree | grep MDSImporte | cut -c 1-${WIDTH} # Chop output after WIDTH chars
Other utilities (e.g. less) can do this for you but may have other side-effects (e.g. prompting you to press the space bar after each page of output).
其他实用程序(例如less)可以为您执行此操作,但可能会产生其他副作用(例如,提示您在每页输出后按空格键)。
Also...
还...
If you're asking how you could determine whether a script is writing to a terminal, file, or pipe, you could do this:
如果您要问如何确定脚本是否正在写入终端、文件或管道,您可以这样做:
[ -t 1 ] && WIDTH=`stty size | cut -d ' ' -f 2`
pstree | grep MDSImporte | cut -c 1-${WIDTH}
This will set WIDTHif and only if standard output is a terminal. If it is, it'll limit the output to WIDTHcharacters (by invoking cut -c 1-80, say). If it's not, it won't limit the output (because cut -c 1-does nothing).
WIDTH当且仅当标准输出是终端时,这将设置。如果是,它会将输出限制为WIDTH字符(例如,通过调用cut -c 1-80)。如果不是,它不会限制输出(因为cut -c 1-什么都不做)。
回答by Karoly Horvath
it checks whether the output is a terminal.
它检查输出是否是终端。
other programs do similar things:
其他程序做类似的事情:
grep --color=auto
ls --color=auto

