Linux 使用 *nix 中的 column 命令格式化列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12133062/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:10:51  来源:igfitidea点击:

Formatting lists with the column command in *nix

linuxbashshellunixformatting

提问by Doug Powers

I'm trying to format a list of entries in bash, and am using the columncommand. However, the -toption defaults to using any whitespace as a delimiter, which does not work for the data I have (it contains spaces and tabs). I can't figure out how to get the -sflag to specify a newline character as the sole column delimiter.

我正在尝试格式化 bash 中的条目列表,并且正在使用该column命令。但是,该-t选项默认使用任何空格作为分隔符,这不适用于我拥有的数据(它包含空格和制表符)。我无法弄清楚如何获取-s标志以将换行符指定为唯一的列分隔符。

采纳答案by ruakh

In theory, to specify a newline, you can use the $'...'notation, which is just like '...'except that it supports C-style escape-sequences:

理论上,要指定换行符,您可以使用$'...'表示法,'...'除了它支持 C 风格的转义序列之外,这就像:

column -t -s $'\n' list-of-entries.txt

However, I don't really understand the purpose of this. A newline is the row delimiter, so a column-delimiter of $'\n'is equivalent to not having any column-delimiter at all:

但是,我真的不明白这样做的目的。换行符是行分隔符,因此列分隔符 of$'\n'相当于根本没有任何列分隔符:

column -t -s '' list-of-entries.txt

which means that the input will be treated as having only one column; so it's equivalent to not using columnat all:

这意味着输入将被视为只有一列;所以它相当于根本不使用column

cat list-of-entries.txt

It seems like you actually don'twant to use the -tflag, because the purpose of the -tflag is to ensure that each line of input becomes one line of output, and it doesn't sound like that's what you want. I'm guessing you want this:

好像你其实希望使用-t标志,因为目的-t标志,以确保输入的每一行变为一行输出,它听起来不像这就是你想要的。我猜你想要这个:

column list-of-entries.txt

which will treat each line of list-of-entries.txtas a value to be put in one cell of the table that columnoutputs.

它将把每一行list-of-entries.txt作为一个值放入column输出表的一个单元格中。

回答by toobsco42

This works to output a pretty print version of a tab delimited file

这可以输出制表符分隔文件的漂亮打印版本

column -t -s $'\t' list-of-entries.txt