列,宽度参数不起作用

column, width parameter not working

我 运行 REHL7 在 column from util-linux 2.23.2

column -V 一起工作

我有一些包含长字符串列的 csv 文件。 我想将 csv 查看为 table,并限制列宽,因为我 通常对抽查长字符串不感兴趣。

cat foo_bar.csv | column -s"," -t -c5

列宽似乎没有限制为 10 个字符。 请问这是bug,还是我做错了看不到?

测试输入,test.csv

co1,col2,col3,col4,col5
1,2,3,longLineOfTextThatIdoNotWantToInspectAndWouldLikeToLimit,5

运行我认为正确的命令:

cat test.csv | column -s"," -t -c5 

co1  col2  col3  col4 col5
1    2     3     longLineOfTextThatIdoNotWantToInspectAndWouldLikeToLimit  5

−c−−columns 选项与您认为的不同。默认, column 查看所有行以找到最长的一行。如果 column 可以容纳 2 of 那些宽度为 80 的线,然后每 2 条线适合一条线:

$ cat file
1 this is a short line
2 this is a short line
3 this line needs to be 39 or less char
4 this line needs to be 39 or less char

$ column file
1 this is a short line                  3 this line needs to be 39 or less char
2 this is a short line                  4 this line needs to be 39 or less char

$ column -x file
1 this is a short line                  2 this is a short line
3 this line needs to be 39 or less char 4 this line needs to be 39 or less char

如果您将 -c 设置为低于 80,那么您获得的可能性就会降低 多于 1 列:

$ column -c70 file
1 this is a short line
2 this is a short line
3 this line needs to be 39 or less char
4 this line needs to be 39 or less char

所以,简单地说,column 不能做你想让它做的事。 Awk 可以做到这一点:

BEGIN {
  FS = ","
}
{
  for (x = 1; x <= NF; x++) {
    printf "%s%s", substr($x, 1, 5), x == NF ? "\n" : "\t"
  }
}

结果:

co1     col2    col3    col4    col5
1       2       3       longL   5

我正在寻找类似问题的解决方案(截断 docker ps 输出中的列)。我不想使用 awk,而是使用 sed 解决了它。

在此示例中,我将 docker ps 的输出限制为每列 30 个字符。

docker ps -a --format "table {{.ID}},{{.Names}},{{.Image}},{{.State}},{{.Networks}}" | \
sed "s/\([^,]\{30\}\)[^,]*//g" | \
column -s "," -t

该模式匹配组中的 30 个非定界符 ([^,]) 字符,后跟其余非定界符字符(如果该列少于 30 个字符,则它不匹配并保留独自的)。替换的只是一组 30 个字符,其余的列被丢弃。

为了好玩,您还可以进行列中截断,以防列的两端都有有用的信息。

docker ps -a --format "table {{.ID}},{{.Names}},{{.Image}},{{.State}},{{.Networks}}" | \
sed "s/\([^,]\{14\}\)[^,]*\([^,]\{14\}\)/../g" | \
column -s "," -t