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
Formatting lists with the column command in *nix
提问by Doug Powers
I'm trying to format a list of entries in bash, and am using the column
command. However, the -t
option 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 -s
flag 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 column
at all:
这意味着输入将被视为只有一列;所以它相当于根本不使用column
:
cat list-of-entries.txt
It seems like you actually don'twant to use the -t
flag, because the purpose of the -t
flag 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.txt
as a value to be put in one cell of the table that column
outputs.
它将把每一行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